Start to implement proper validation of HTTP interface
This commit is contained in:
parent
42ee69b1a2
commit
ba52458fd6
3 changed files with 32 additions and 42 deletions
1
Makefile
1
Makefile
|
@ -22,6 +22,7 @@ test:
|
||||||
|
|
||||||
@echo Integration tests
|
@echo Integration tests
|
||||||
docker-compose up --build --exit-code-from=rove-tests --abort-on-container-exit rove-tests
|
docker-compose up --build --exit-code-from=rove-tests --abort-on-container-exit rove-tests
|
||||||
|
docker-compose down
|
||||||
go tool cover -html=/tmp/coverage-data/c.out -o /tmp/coverage.html
|
go tool cover -html=/tmp/coverage-data/c.out -o /tmp/coverage.html
|
||||||
|
|
||||||
@echo Done, coverage data can be found in /tmp/coverage.html
|
@echo Done, coverage data can be found in /tmp/coverage.html
|
||||||
|
|
|
@ -7,42 +7,23 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mdiluz/rove/pkg/rove"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Server is a simple wrapper to a server path
|
// Server is a simple wrapper to a server path
|
||||||
type Server string
|
type Server string
|
||||||
|
|
||||||
// Get performs a Get request
|
// Request performs a HTTP
|
||||||
func (s Server) Get(path string, out interface{}) error {
|
func (s Server) Request(method, path string, in, out interface{}) error {
|
||||||
u := url.URL{
|
u := url.URL{
|
||||||
Scheme: "http",
|
Scheme: "http",
|
||||||
Host: string(s),
|
Host: fmt.Sprintf("%s:8080", string(s)),
|
||||||
Path: path,
|
|
||||||
}
|
|
||||||
if resp, err := http.Get(u.String()); err != nil {
|
|
||||||
return err
|
|
||||||
|
|
||||||
} else if resp.StatusCode != http.StatusOK {
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to read response body to code %d", resp.StatusCode)
|
|
||||||
}
|
|
||||||
return fmt.Errorf("http returned status %d: %s", resp.StatusCode, string(body))
|
|
||||||
|
|
||||||
} else {
|
|
||||||
return json.NewDecoder(resp.Body).Decode(out)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Post performs a Post request
|
|
||||||
func (s Server) Post(path string, in, out interface{}) error {
|
|
||||||
u := url.URL{
|
|
||||||
Scheme: "http",
|
|
||||||
Host: string(s),
|
|
||||||
Path: path,
|
Path: path,
|
||||||
}
|
}
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
@ -54,7 +35,7 @@ func (s Server) Post(path string, in, out interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up the request
|
// Set up the request
|
||||||
req, err := http.NewRequest("POST", u.String(), bytes.NewReader(marshalled))
|
req, err := http.NewRequest(method, u.String(), bytes.NewReader(marshalled))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -76,9 +57,14 @@ func (s Server) Post(path string, in, out interface{}) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var serv = Server(os.Getenv("ROVE_GRPC"))
|
var serv = Server(os.Getenv("ROVE_HTTP"))
|
||||||
|
|
||||||
func TestServer_Status(t *testing.T) {
|
func TestServer_Status(t *testing.T) {
|
||||||
|
req := &rove.StatusRequest{}
|
||||||
|
resp := &rove.StatusResponse{}
|
||||||
|
if err := serv.Request("GET", "status", req, resp); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServer_Register(t *testing.T) {
|
func TestServer_Register(t *testing.T) {
|
||||||
|
|
|
@ -18,18 +18,6 @@ services:
|
||||||
- persistent-data:/mnt/rove-server:rw
|
- persistent-data:/mnt/rove-server:rw
|
||||||
command: [ ./rove-accountant ]
|
command: [ ./rove-accountant ]
|
||||||
|
|
||||||
rove-reverse-proxy:
|
|
||||||
build:
|
|
||||||
context: .
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
image: rove:latest
|
|
||||||
ports:
|
|
||||||
- "8080:8080"
|
|
||||||
environment:
|
|
||||||
- PORT=8080
|
|
||||||
- ROVE_GRPC=rove-server:9090
|
|
||||||
command: [ ./rove-reverse-proxy ]
|
|
||||||
|
|
||||||
rove-docs:
|
rove-docs:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
|
@ -41,7 +29,7 @@ services:
|
||||||
- PORT=80
|
- PORT=80
|
||||||
|
|
||||||
rove-server:
|
rove-server:
|
||||||
depends_on: [ rove-accountant, rove-reverse-proxy, rove-docs]
|
depends_on: [ rove-accountant ]
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
|
@ -54,17 +42,32 @@ services:
|
||||||
- ROVE_ACCOUNTANT_GRPC=rove-accountant:9091
|
- ROVE_ACCOUNTANT_GRPC=rove-accountant:9091
|
||||||
volumes:
|
volumes:
|
||||||
- persistent-data:/mnt/rove-server:rw
|
- persistent-data:/mnt/rove-server:rw
|
||||||
|
command: [ "./script/wait-for-it.sh", "rove-accountant:9091", "--", "./rove-server"]
|
||||||
|
|
||||||
|
rove:
|
||||||
|
depends_on: [ rove-server, rove-docs ]
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
image: rove:latest
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
environment:
|
||||||
|
- PORT=8080
|
||||||
|
- ROVE_GRPC=rove-server:9090
|
||||||
|
command: [ "./script/wait-for-it.sh", "rove-server:9090", "--", "./rove-reverse-proxy" ]
|
||||||
|
|
||||||
rove-tests:
|
rove-tests:
|
||||||
depends_on: [ rove-server ]
|
depends_on: [ rove ]
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
image: rove:latest
|
image: rove:latest
|
||||||
environment:
|
environment:
|
||||||
- ROVE_ACCOUNTANT_GRPC=rove-accountant:9091
|
- ROVE_ACCOUNTANT_GRPC=rove-accountant:9091
|
||||||
|
- ROVE_HTTP=rove
|
||||||
- ROVE_GRPC=rove-server
|
- ROVE_GRPC=rove-server
|
||||||
command: [ "go", "test", "-v", "./...", "--tags=integration", "-cover", "-coverprofile=/mnt/coverage-data/c.out", "-count", "1" ]
|
command: [ "./script/wait-for-it.sh", "rove:8080", "--", "go", "test", "-v", "./...", "--tags=integration", "-cover", "-coverprofile=/mnt/coverage-data/c.out", "-count", "1" ]
|
||||||
volumes:
|
volumes:
|
||||||
- /tmp/coverage-data:/mnt/coverage-data:rw
|
- /tmp/coverage-data:/mnt/coverage-data:rw
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue