diff --git a/cmd/test.go b/cmd/test.go index 029c289..214c69c 100644 --- a/cmd/test.go +++ b/cmd/test.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "net/url" "strings" accord "github.com/datascienceinc/accord/pkg" @@ -34,29 +35,52 @@ var testCmd = &cobra.Command{ }, } +type byteBufferReadCloser struct { + bytes.Buffer +} + +func (b *byteBufferReadCloser) Close() error { + return nil +} + func init() { RootCmd.AddCommand(testCmd) client = httptest.NewClient() } -func server(host, uri string) string { - return fmt.Sprintf("%s%s", host, uri) +func server(host, uri string, query map[string]string) *url.URL { + + url := url.URL{Host: host, Path: uri} + // if there are query parameters to add to the url + if len(query) != 0 { + // grab the query object + q := url.Query() + // loop over the endpoint query specifications + for k, v := range query { + // add the query to the url + q.Add(k, v) + } + // assign the query back to the url + url.RawQuery = q.Encode() + } + + return &url } func test(host string) { ctx.ProcessEndpoints(func(ep *accord.Endpoint) { - var buf bytes.Buffer + var buf byteBufferReadCloser if ep.Request != nil { buf = parseBody(ep.Request.Headers, ep.Request.Body) } - req, err := http.NewRequest(ep.Method, server(host, ep.URI), &buf) - if err != nil { - color.Red("ERR: %s\n", err) - return + req := &http.Request{ + URL: server(host, ep.URI, ep.Request.Query), + Method: ep.Method, + Body: &buf, } - err = client.Evaluate(req, ep.Response) + err := client.Evaluate(req, ep.Response) if err != nil { color.Red("ERR: %s\n", err) return @@ -97,8 +121,8 @@ func compareResponse(resp *http.Response, expect *accord.Response) error { return nil } -func parseBody(h http.Header, i interface{}) bytes.Buffer { - var buf bytes.Buffer +func parseBody(h http.Header, i interface{}) byteBufferReadCloser { + var buf byteBufferReadCloser if i == nil { i = "" } diff --git a/examples/test.hcl b/examples/test.hcl index af9c1be..a856ec2 100644 --- a/examples/test.hcl +++ b/examples/test.hcl @@ -8,6 +8,11 @@ endpoint "/users" { "test": "value" } EOF + + query { + hello = "world" + goodbye = "moon" + } } response { diff --git a/pkg/parse.go b/pkg/parse.go index 4ae3d8f..1f93dcb 100644 --- a/pkg/parse.go +++ b/pkg/parse.go @@ -239,9 +239,20 @@ func loadRequest(list *ast.ObjectList) (*Request, error) { } } + var query map[string]string + if o := listVal.Filter("query"); len(o.Items) > 0 { + err := hcl.DecodeObject(&query, o.Items[0].Val) + if err != nil { + return nil, fmt.Errorf( + "Error parsing query: %s", + err) + } + } + result = &Request{ Body: body, Headers: headers, + Query: query, } return result, nil diff --git a/pkg/parse_test.go b/pkg/parse_test.go new file mode 100644 index 0000000..d3a20c7 --- /dev/null +++ b/pkg/parse_test.go @@ -0,0 +1,47 @@ +package accord + +import ( + "github.com/hashicorp/hcl" + "testing" +) + +func TestParseHandlesQueryParams(t *testing.T) { + // an accord config with a query entry in the request block + config := ` + endpoint "/users" { + method = "POST" + + request { + + query { + hello = "world" + goodbye = "moon" + } + } + } + ` + // parse the string + ast, err := hcl.Parse(config) + // if there was something wrong + if err != nil { + t.Error(err.Error()) + panic(err) + } + + // create the accord config from the ast + contract, err := parse(ast) + // if something went wrong + if err != nil { + t.Error(err.Error()) + panic(err) + } + // grab the query log from the config request + query := contract.Endpoints[0].Request.Query + // check the values of the query map + if query["hello"] != "world" { + // the test failed + t.Errorf( + "Incorrect value for hello in request query. Found %v, wanted %v", + query["hello"], "world") + } +} diff --git a/pkg/response.go b/pkg/response.go index 19a2c63..d58933c 100644 --- a/pkg/response.go +++ b/pkg/response.go @@ -12,5 +12,6 @@ type Response struct { // Request ... type Request struct { Headers http.Header - Body interface{} `hcl:"body"` + Body interface{} `hcl:"body"` + Query map[string]string `hcl:"query"` }