This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
response_test.go
102 lines (84 loc) · 3.1 KB
/
response_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/Shopify/toxiproxy"
"github.com/Shopify/toxiproxy/toxics"
"github.com/stretchr/testify/assert"
)
func ToxicToJson(t *testing.T, name, typeName, stream string, toxic toxics.Toxic) io.Reader {
data := map[string]interface{}{
"name": name,
"type": typeName,
"stream": stream,
"attributes": toxic,
}
request, err := json.Marshal(data)
if err != nil {
t.Errorf("Failed to marshal toxic for api (1): %v", toxic)
}
return bytes.NewReader(request)
}
func DoResponseTest(t *testing.T, downResponse *HttpResponseToxic) (int, string, string) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"hello": "world"}`)
}))
defer ts.Close()
proxy := toxiproxy.NewProxy()
proxy.Name = "test"
proxy.Listen = "localhost:0"
proxy.Upstream = ts.Listener.Addr().String()
proxy.Start()
if downResponse == nil {
downResponse = &HttpResponseToxic{}
} else {
_, err := proxy.Toxics.AddToxicJson(ToxicToJson(t, "response_down", "response", "downstream", downResponse))
if err != nil {
t.Error("AddToxicJson returned error:", err)
}
}
response, err := http.Get("http://" + proxy.Listen)
if err != nil {
log.Fatal(err)
}
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
return response.StatusCode, response.Status, string(responseBody)
}
func TestDownstreamResponse(t *testing.T) {
assert := assert.New(t)
code, status, body := DoResponseTest(t, nil)
assert.Equal(200, code, "This test should not have manipultated the status code")
assert.Equal(`200 OK`, status, "This test should not have manipultated the status")
assert.Equal(`{"hello": "world"}`, body, "This test should not have manipultated the body")
}
func TestHttpStatusCode(t *testing.T) {
assert := assert.New(t)
code, status, body := DoResponseTest(t, &HttpResponseToxic{HttpStatusCode: 418})
assert.Equal(418, code, "This test should have manipultated the status code")
assert.Equal(`418 200 OK`, status, "This test should not have manipultated the status")
assert.Equal(`{"hello": "world"}`, body, "This test should not have manipultated the body")
}
func TestHttpStatusText(t *testing.T) {
assert := assert.New(t)
code, status, body := DoResponseTest(t, &HttpResponseToxic{HttpStatusText: "FOO"})
assert.Equal(200, code, "This test should not have manipultated the status code")
assert.Equal(`200 FOO`, status, "This test should have manipultated the status")
assert.Equal(`{"hello": "world"}`, body, "This test should not have manipultated the body")
}
func TestHttpBody(t *testing.T) {
assert := assert.New(t)
code, status, body := DoResponseTest(t, &HttpResponseToxic{HttpBody: "{'foo': 'bar'}"})
assert.Equal(200, code, "This test should not have manipultated the status code")
assert.Equal(`200 OK`, status, "This test should not have manipultated the status")
assert.Equal(`{'foo': 'bar'}`, body, "This test should have manipultated the body")
}