-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsimple_get_with_testing_test.go
43 lines (36 loc) · 1.13 KB
/
simple_get_with_testing_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// nolint dupl
package functional_tests
import (
"io/ioutil"
"net/http"
"testing"
"github.com/maxcnunes/httpfake"
)
// TestSimpleGetWithTesting tests a fake server handling a GET request
func TestSimpleGetWithTesting(t *testing.T) {
fakeService := httpfake.New(httpfake.WithTesting(t))
defer fakeService.Close()
// register a handler for our fake service
fakeService.NewHandler().
Get("/users?movie=dreamers").
AssertQueryValue("movie", "dreamers").
Reply(200).
BodyString(`[{"username": "dreamer","movie": "dreamers"}]`)
res, err := http.Get(fakeService.ResolveURL("/users?movie=dreamers"))
if err != nil {
t.Fatal(err)
}
defer res.Body.Close() // nolint errcheck
// Check the status code is what we expect
if status := res.StatusCode; status != 200 {
t.Errorf("request returned wrong status code: got %v want %v",
status, 200)
}
// Check the response body is what we expect
expected := `[{"username": "dreamer","movie": "dreamers"}]`
body, _ := ioutil.ReadAll(res.Body)
if bodyString := string(body); bodyString != expected {
t.Errorf("request returned unexpected body: got %v want %v",
bodyString, expected)
}
}