Finish HTTP tests and adjust APIs to allow them to pass

This commit is contained in:
Marc Di Luzio 2020-06-13 13:18:22 +01:00
parent ba52458fd6
commit 187a0a6165
6 changed files with 117 additions and 48 deletions

View file

@ -13,7 +13,9 @@ import (
"os"
"testing"
"github.com/google/uuid"
"github.com/mdiluz/rove/pkg/rove"
"github.com/stretchr/testify/assert"
)
// Server is a simple wrapper to a server path
@ -68,13 +70,59 @@ func TestServer_Status(t *testing.T) {
}
func TestServer_Register(t *testing.T) {
req := &rove.RegisterRequest{Name: uuid.New().String()}
resp := &rove.RegisterResponse{}
err := serv.Request("POST", "register", req, resp)
assert.NoError(t, err, "First register attempt should pass")
err = serv.Request("POST", "register", req, resp)
assert.Error(t, err, "Second identical register attempt should fail")
}
func TestServer_Command(t *testing.T) {
acc := uuid.New().String()
err := serv.Request("POST", "register", &rove.RegisterRequest{Name: acc}, &rove.RegisterResponse{})
assert.NoError(t, err, "First register attempt should pass")
err = serv.Request("POST", "commands", &rove.CommandsRequest{
Account: acc,
Commands: []*rove.Command{
{
Command: "move",
Bearing: "NE",
Duration: 1,
},
},
}, &rove.CommandsResponse{})
assert.NoError(t, err, "Commands should should pass")
}
func TestServer_Radar(t *testing.T) {
acc := uuid.New().String()
err := serv.Request("POST", "register", &rove.RegisterRequest{Name: acc}, &rove.RegisterResponse{})
assert.NoError(t, err, "First register attempt should pass")
resp := &rove.RadarResponse{}
err = serv.Request("POST", "radar", &rove.RadarRequest{
Account: acc,
}, resp)
assert.NoError(t, err, "Radar sould pass should pass")
assert.NotZero(t, resp.Range, "Radar should return valid range")
w := int(resp.Range*2 + 1)
assert.Equal(t, w*w, len(resp.Tiles), "radar should return correct number of tiles")
}
func TestServer_Rover(t *testing.T) {
acc := uuid.New().String()
err := serv.Request("POST", "register", &rove.RegisterRequest{Name: acc}, &rove.RegisterResponse{})
assert.NoError(t, err, "First register attempt should pass")
resp := &rove.RoverResponse{}
err = serv.Request("POST", "rover", &rove.RoverRequest{
Account: acc,
}, resp)
assert.NoError(t, err, "Rover sould pass should pass")
assert.NotZero(t, resp.Range, "Rover should return valid range")
assert.NotZero(t, len(resp.Name), "Rover should return valid name")
assert.NotZero(t, resp.Speed, "Rover should return valid speed")
assert.NotZero(t, resp.Position, "Rover should return valid position")
}

View file

@ -69,6 +69,8 @@ func (s *Server) Rover(ctx context.Context, req *rove.RoverRequest) (*rove.Rover
X: int32(attrib.Pos.X),
Y: int32(attrib.Pos.Y),
},
Speed: int32(attrib.Speed),
Range: int32(attrib.Range),
}
}
return response, nil

View file

@ -775,7 +775,7 @@ var file_rove_rove_proto_rawDesc = []byte{
0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x76,
0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x22, 0x24, 0x0a, 0x06, 0x56, 0x65, 0x63, 0x74, 0x6f, 0x72,
0x12, 0x0c, 0x0a, 0x01, 0x78, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x78, 0x12, 0x0c,
0x0a, 0x01, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x79, 0x32, 0xf2, 0x02, 0x0a,
0x0a, 0x01, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x01, 0x79, 0x32, 0xf8, 0x02, 0x0a,
0x04, 0x52, 0x6f, 0x76, 0x65, 0x12, 0x44, 0x0a, 0x06, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12,
0x13, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x65, 0x71,
0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x2e, 0x53, 0x74, 0x61, 0x74,
@ -790,18 +790,19 @@ var file_rove_rove_proto_rawDesc = []byte{
0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a,
0x16, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x73, 0x52,
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x14, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x0e, 0x22,
0x09, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x73, 0x3a, 0x01, 0x2a, 0x12, 0x40, 0x0a,
0x09, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x73, 0x3a, 0x01, 0x2a, 0x12, 0x43, 0x0a,
0x05, 0x52, 0x61, 0x64, 0x61, 0x72, 0x12, 0x12, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x2e, 0x52, 0x61,
0x64, 0x61, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x72, 0x6f, 0x76,
0x65, 0x2e, 0x52, 0x61, 0x64, 0x61, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22,
0x0e, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x08, 0x12, 0x06, 0x2f, 0x72, 0x61, 0x64, 0x61, 0x72, 0x12,
0x40, 0x0a, 0x05, 0x52, 0x6f, 0x76, 0x65, 0x72, 0x12, 0x12, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x2e,
0x52, 0x6f, 0x76, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x72,
0x6f, 0x76, 0x65, 0x2e, 0x52, 0x6f, 0x76, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
0x65, 0x22, 0x0e, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x08, 0x12, 0x06, 0x2f, 0x72, 0x6f, 0x76, 0x65,
0x72, 0x42, 0x21, 0x5a, 0x1f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
0x6d, 0x64, 0x69, 0x6c, 0x75, 0x7a, 0x2f, 0x72, 0x6f, 0x76, 0x65, 0x2f, 0x70, 0x6b, 0x67, 0x2f,
0x72, 0x6f, 0x76, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x11, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x0b, 0x22, 0x06, 0x2f, 0x72, 0x61, 0x64, 0x61, 0x72, 0x3a,
0x01, 0x2a, 0x12, 0x43, 0x0a, 0x05, 0x52, 0x6f, 0x76, 0x65, 0x72, 0x12, 0x12, 0x2e, 0x72, 0x6f,
0x76, 0x65, 0x2e, 0x52, 0x6f, 0x76, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a,
0x13, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x2e, 0x52, 0x6f, 0x76, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70,
0x6f, 0x6e, 0x73, 0x65, 0x22, 0x11, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x0b, 0x22, 0x06, 0x2f, 0x72,
0x6f, 0x76, 0x65, 0x72, 0x3a, 0x01, 0x2a, 0x42, 0x21, 0x5a, 0x1f, 0x67, 0x69, 0x74, 0x68, 0x75,
0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6d, 0x64, 0x69, 0x6c, 0x75, 0x7a, 0x2f, 0x72, 0x6f, 0x76,
0x65, 0x2f, 0x70, 0x6b, 0x67, 0x2f, 0x72, 0x6f, 0x76, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x33,
}
var (

View file

@ -117,18 +117,15 @@ func local_request_Rove_Commands_0(ctx context.Context, marshaler runtime.Marsha
}
var (
filter_Rove_Radar_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)}
)
func request_Rove_Radar_0(ctx context.Context, marshaler runtime.Marshaler, client RoveClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq RadarRequest
var metadata runtime.ServerMetadata
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
newReader, berr := utilities.IOReaderFactory(req.Body)
if berr != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Rove_Radar_0); err != nil {
if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
@ -141,10 +138,11 @@ func local_request_Rove_Radar_0(ctx context.Context, marshaler runtime.Marshaler
var protoReq RadarRequest
var metadata runtime.ServerMetadata
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
newReader, berr := utilities.IOReaderFactory(req.Body)
if berr != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Rove_Radar_0); err != nil {
if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
@ -153,18 +151,15 @@ func local_request_Rove_Radar_0(ctx context.Context, marshaler runtime.Marshaler
}
var (
filter_Rove_Rover_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)}
)
func request_Rove_Rover_0(ctx context.Context, marshaler runtime.Marshaler, client RoveClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq RoverRequest
var metadata runtime.ServerMetadata
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
newReader, berr := utilities.IOReaderFactory(req.Body)
if berr != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Rove_Rover_0); err != nil {
if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
@ -177,10 +172,11 @@ func local_request_Rove_Rover_0(ctx context.Context, marshaler runtime.Marshaler
var protoReq RoverRequest
var metadata runtime.ServerMetadata
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
newReader, berr := utilities.IOReaderFactory(req.Body)
if berr != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Rove_Rover_0); err != nil {
if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
@ -254,7 +250,7 @@ func RegisterRoveHandlerServer(ctx context.Context, mux *runtime.ServeMux, serve
})
mux.Handle("GET", pattern_Rove_Radar_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
mux.Handle("POST", pattern_Rove_Radar_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
@ -274,7 +270,7 @@ func RegisterRoveHandlerServer(ctx context.Context, mux *runtime.ServeMux, serve
})
mux.Handle("GET", pattern_Rove_Rover_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
mux.Handle("POST", pattern_Rove_Rover_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
@ -395,7 +391,7 @@ func RegisterRoveHandlerClient(ctx context.Context, mux *runtime.ServeMux, clien
})
mux.Handle("GET", pattern_Rove_Radar_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
mux.Handle("POST", pattern_Rove_Radar_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
@ -415,7 +411,7 @@ func RegisterRoveHandlerClient(ctx context.Context, mux *runtime.ServeMux, clien
})
mux.Handle("GET", pattern_Rove_Rover_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
mux.Handle("POST", pattern_Rove_Rover_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)

View file

@ -46,7 +46,7 @@
}
},
"/radar": {
"get": {
"post": {
"summary": "Get radar information",
"description": "Gets the radar output for the given rover",
"operationId": "Rove_Radar",
@ -66,11 +66,12 @@
},
"parameters": [
{
"name": "account",
"description": "The account for this request.",
"in": "query",
"required": false,
"type": "string"
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/roveRadarRequest"
}
}
],
"tags": [
@ -113,7 +114,7 @@
}
},
"/rover": {
"get": {
"post": {
"summary": "Get rover information",
"description": "Gets information for the account's rover",
"operationId": "Rove_Rover",
@ -133,11 +134,12 @@
},
"parameters": [
{
"name": "account",
"description": "The account for this request.",
"in": "query",
"required": false,
"type": "string"
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/roveRoverRequest"
}
}
],
"tags": [
@ -242,6 +244,15 @@
"type": "object",
"title": "Empty placeholder"
},
"roveRadarRequest": {
"type": "object",
"properties": {
"account": {
"type": "string",
"title": "The account for this request"
}
}
},
"roveRadarResponse": {
"type": "object",
"properties": {
@ -270,6 +281,15 @@
"type": "object",
"title": "Empty placeholder"
},
"roveRoverRequest": {
"type": "object",
"properties": {
"account": {
"type": "string",
"title": "The account for this request"
}
}
},
"roveRoverResponse": {
"type": "object",
"properties": {

View file

@ -41,7 +41,8 @@ service Rove {
// Gets the radar output for the given rover
rpc Radar(RadarRequest) returns (RadarResponse) {
option (google.api.http) = {
get: "/radar"
post: "/radar"
body: "*"
};
}
@ -50,7 +51,8 @@ service Rove {
// Gets information for the account's rover
rpc Rover(RoverRequest) returns (RoverResponse) {
option (google.api.http) = {
get: "/rover"
post: "/rover"
body: "*"
};
}
}