...
1
10
11 package requests
12
13 import (
14 "net/http"
15 "net/http/httptest"
16 "reflect"
17 "testing"
18
19 "gitlab.hexacode.org/go-libs/hctypes"
20 )
21
22 func TestRequest(t *testing.T) {
23
24 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
25
26 w.WriteHeader(StatusOK)
27 w.Write([]byte("Mock Response"))
28 }))
29 defer server.Close()
30
31
32 in := &Input{
33 Method: "GET",
34 URL: server.URL,
35 URLParams: nil,
36 Headers: nil,
37 Body: hctypes.Buffer{},
38 }
39
40
41 output, err := Request(in)
42
43
44 expectedOutput := &Output{
45 Status: StatusOK,
46 StatusText: StatusText(StatusOK),
47 Headers: nil,
48 Body: hctypes.Buffer("Mock Response"),
49 }
50
51 if !reflect.DeepEqual(output.Status, expectedOutput.Status) {
52 t.Errorf("Unexpected `output.Status`. Expected \"%v\", got \"%v\"", expectedOutput.Status, output.Status)
53 }
54 if !reflect.DeepEqual(output.StatusText, expectedOutput.StatusText) {
55 t.Errorf("Unexpected `output.StatusText`. Expected \"%v\", got \"%v\"", expectedOutput.StatusText, output.StatusText)
56 }
57 if !output.Body.Equal(expectedOutput.Body) {
58 t.Errorf("Unexpected `output.Body`. Expected \"%v\", got \"%v\"", expectedOutput.Body.ToString(), output.Body.ToString())
59 }
60
61
62 if err != nil {
63 t.Errorf("Unexpected error: %v", err)
64 }
65 }
66
View as plain text