Skip to content

Commit 1970c2b

Browse files
alexhornbakesjkaliski
authored andcommitted
Assert http requests (#42)
* add assert helpers for testing http requests * add basic tests for AssertHTTPRequest and AssertHTTPRequestOut * fix golint issues
1 parent 9fd8f32 commit 1970c2b

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

assert.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,48 @@ func Assert(t *testing.T, id string, a Assertable) {
2727

2828
// AssertHTTPResponse asserts the value of an http.Response.
2929
func AssertHTTPResponse(t *testing.T, id string, w *http.Response) {
30-
config, err := getConfig()
30+
body, err := httputil.DumpResponse(w, true)
3131
if err != nil {
3232
t.Fatal(err)
3333
}
3434

35-
body, err := httputil.DumpResponse(w, true)
35+
assertHTTP(t, id, body, contentTypeIsJSON(w.Header.Get("Content-Type")))
36+
}
37+
38+
// AssertHTTPRequestOut asserts the value of an http.Request.
39+
// Intended for use when testing outgoing client requests
40+
// See https://golang.org/pkg/net/http/httputil/#DumpRequestOut for more
41+
func AssertHTTPRequestOut(t *testing.T, id string, r *http.Request) {
42+
body, err := httputil.DumpRequestOut(r, true)
3643
if err != nil {
3744
t.Fatal(err)
3845
}
3946

40-
data := string(body)
47+
assertHTTP(t, id, body, contentTypeIsJSON(r.Header.Get("Content-Type")))
48+
}
4149

42-
contentType := w.Header.Get("Content-Type")
50+
// AssertHTTPRequest asserts the value of an http.Request.
51+
// Intended for use when testing incoming client requests
52+
// See https://golang.org/pkg/net/http/httputil/#DumpRequest for more
53+
func AssertHTTPRequest(t *testing.T, id string, r *http.Request) {
54+
body, err := httputil.DumpRequest(r, true)
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
59+
assertHTTP(t, id, body, contentTypeIsJSON(r.Header.Get("Content-Type")))
60+
}
61+
62+
func assertHTTP(t *testing.T, id string, body []byte, isJSON bool) {
63+
config, err := getConfig()
64+
if err != nil {
65+
t.Fatal(err)
66+
}
67+
68+
data := string(body)
4369

4470
// If the response body is JSON, indent.
45-
if contentTypeIsJSON(contentType) {
71+
if isJSON {
4672
lines := strings.Split(strings.TrimSpace(data), "\n")
4773
jsonStr := lines[len(lines)-1]
4874

example/__snapshots__/example.snapshot

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@ Content-Type: application/json
77
"foo": "foobar"
88
}
99

10+
/* snapshot: http client request */
11+
GET / HTTP/1.1
12+
Host: example.com
13+
User-Agent: Go-http-client/1.1
14+
Content-Length: 31
15+
Content-Type: application/json
16+
X-Expected-Header: expected header value
17+
Accept-Encoding: gzip
18+
19+
{
20+
"message": "expected message"
21+
}
22+
23+
/* snapshot: http server request */
24+
POST / HTTP/1.1
25+
Accept-Encoding: gzip
26+
Content-Length: 31
27+
Content-Type: application/json
28+
User-Agent: Go-http-client/1.1
29+
30+
{
31+
"message": "expected message"
32+
}
33+
1034
/* snapshot: reader */
1135
Hello World.
1236

example/main_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"net/http"
45
"net/http/httptest"
56
"os"
7+
"strings"
68
"testing"
79

810
"github.com/beme/abide"
@@ -41,3 +43,20 @@ func TestReader(t *testing.T) {
4143
res := w.Result()
4244
abide.AssertReader(t, "reader", res.Body)
4345
}
46+
47+
func TestAssertHTTPRequestOut(t *testing.T) {
48+
req := httptest.NewRequest(http.MethodGet, "http://example.com", strings.NewReader(`{"message": "expected message"}`))
49+
req.Header.Set("Content-Type", "application/json")
50+
req.Header.Set("X-Expected-Header", "expected header value")
51+
52+
abide.AssertHTTPRequestOut(t, "http client request", req)
53+
}
54+
55+
func TestAssertHTTPRequest(t *testing.T) {
56+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
57+
r.Host = "" // httptest servers are spawned on random ports, prevent that from being in the snapshot.
58+
abide.AssertHTTPRequest(t, "http server request", r)
59+
}))
60+
61+
http.Post(server.URL, "application/json", strings.NewReader(`{"message": "expected message"}`))
62+
}

0 commit comments

Comments
 (0)