Skip to content

Commit

Permalink
Merge pull request #10 from datascienceinc/feature/query-string
Browse files Browse the repository at this point in the history
Feature/query string
  • Loading branch information
ChrisMcKenzie authored Feb 1, 2017
2 parents 50503c2 + 475b3bd commit 4ad39d1
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 11 deletions.
44 changes: 34 additions & 10 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"

accord "github.com/datascienceinc/accord/pkg"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = ""
}
Expand Down
5 changes: 5 additions & 0 deletions examples/test.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ endpoint "/users" {
"test": "value"
}
EOF

query {
hello = "world"
goodbye = "moon"
}
}

response {
Expand Down
11 changes: 11 additions & 0 deletions pkg/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions pkg/parse_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
3 changes: 2 additions & 1 deletion pkg/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 4ad39d1

Please sign in to comment.