Skip to content

Commit

Permalink
Merge pull request #6 from datascienceinc/fix/tests
Browse files Browse the repository at this point in the history
added test to httptest package
  • Loading branch information
ChrisMcKenzie authored Oct 26, 2016
2 parents 75ca05b + 4f88e84 commit 50503c2
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 5 deletions.
5 changes: 3 additions & 2 deletions pkg/httptest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package httptest

import (
"net/http"
"strconv"

accord "github.com/datascienceinc/accord/pkg"
)
Expand All @@ -25,15 +26,15 @@ func (c *Client) Evaluate(req *http.Request, expected *accord.Response) error {

// first things first is the status code right?
if resp.StatusCode != expected.Code {
return diffError(string(expected.Code), string(resp.StatusCode))
return diffError("Status", strconv.Itoa(expected.Code), strconv.Itoa(resp.StatusCode))
}

// Lets go through all the headers in the expectation and make sure they match
// we dont want to test that all headers match and exist because some servers
// will send extra headers (ie. X-Powered-By)
for h := range expected.Headers {
if expected.Headers.Get(h) != resp.Header.Get(h) {
return diffError(expected.Headers.Get(h), resp.Header.Get(h))
return diffError("Header", expected.Headers.Get(h), resp.Header.Get(h))
}
}

Expand Down
151 changes: 151 additions & 0 deletions pkg/httptest/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package httptest

import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

accord "github.com/datascienceinc/accord/pkg"
)

func httpHandler(method string, res *accord.Response, t *testing.T) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
if req.Method != method {
t.Errorf("Expected method to be %s, got %s", method, req.Method)
}

for key := range res.Headers {
w.Header().Set(key, res.Headers.Get(key))
}

w.WriteHeader(res.Code)

resp := parseBody(res.Headers, res.Body)
w.Write(resp.Bytes())
}
}

func newRequest(method, url string, req *accord.Request) (*http.Request, error) {
var buf bytes.Buffer
if req != nil {
buf = parseBody(req.Headers, req.Body)
}

r, err := http.NewRequest(method, url, &buf)
if err != nil {
return nil, err
}

return r, nil
}

func TestHttpClient(t *testing.T) {
cases := []struct {
name string
errNil bool
method string
url string
request *accord.Request
response *accord.Response
expected *accord.Response
}{
{
name: "Basic Request",
errNil: true,
method: "GET",
url: "/test",
request: &accord.Request{},
response: &accord.Response{},
expected: &accord.Response{},
},
{
name: "Failing Status Code request",
errNil: false,
method: "GET",
url: "/test",
request: &accord.Request{},
response: &accord.Response{},
expected: &accord.Response{
Code: 400,
},
},
{
name: "Failing Header request",
errNil: false,
method: "GET",
url: "/test",
request: &accord.Request{},
response: &accord.Response{},
expected: &accord.Response{
Headers: http.Header(map[string][]string{
"X-My-Header": []string{"test"},
}),
},
},
{
name: "Failing Body request",
errNil: false,
method: "GET",
url: "/test",
request: &accord.Request{},
response: &accord.Response{},
expected: &accord.Response{
Body: "test",
},
},
{
name: "JSON body request",
errNil: true,
method: "GET",
url: "/test",
request: &accord.Request{},
response: &accord.Response{
Headers: http.Header(map[string][]string{
"Content-Type": []string{"application/json"},
}),
Body: map[string]string{
"test": "yes",
},
},
expected: &accord.Response{
Headers: http.Header(map[string][]string{
"Content-Type": []string{"application/json"},
}),
Body: map[string]string{
"test": "yes",
},
},
},
}

client := NewClient()
for _, c := range cases {
fmt.Printf("==> %s\n", c.name)
server := httptest.NewServer(
http.HandlerFunc(
httpHandler(
c.method,
c.response,
t,
),
),
)
defer server.Close()

req, err := newRequest(
c.method,
strings.Join([]string{server.URL, c.url}, "/"),
c.request)
if err != nil {
t.Error(err)
}

err = client.Evaluate(req, c.expected)
if c.errNil && err != nil {
t.Errorf("Expected evaluate to return nil got:\n%s\n", err)
}
}
}
6 changes: 3 additions & 3 deletions pkg/httptest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
"github.com/pmezard/go-difflib/difflib"
)

func diffError(a string, b string) error {
func diffError(typ, a, b string) error {
diff := difflib.ContextDiff{
A: difflib.SplitLines(a),
B: difflib.SplitLines(b),
FromFile: "Actual",
ToFile: "Expectation",
FromFile: fmt.Sprintf("Actual %s", typ),
ToFile: fmt.Sprintf("Expected %s", typ),
Context: 3,
Eol: "\n",
}
Expand Down

0 comments on commit 50503c2

Please sign in to comment.