|
| 1 | +package protocol |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestResponseCodeFlag(t *testing.T) { |
| 11 | + tests := []int{200, 404, 201, 500} |
| 12 | + |
| 13 | + for _, respCode := range tests { |
| 14 | + httpServer := Http{ResponseCode: respCode} |
| 15 | + srv := httptest.NewServer(httpServer.routes()) |
| 16 | + req, err := http.NewRequest(http.MethodGet, srv.URL+"/", nil) |
| 17 | + if err != nil { |
| 18 | + t.Error(err) |
| 19 | + } |
| 20 | + |
| 21 | + resp, err := http.DefaultClient.Do(req) |
| 22 | + if err != nil { |
| 23 | + t.Error(err) |
| 24 | + } |
| 25 | + |
| 26 | + if resp.StatusCode != respCode { |
| 27 | + t.Errorf("Expected %d, got %d", respCode, resp.StatusCode) |
| 28 | + } |
| 29 | + resp.Body.Close() |
| 30 | + srv.Close() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestLogRequestOneRenderer(t *testing.T) { |
| 35 | + testTable := []struct { |
| 36 | + method string |
| 37 | + path string |
| 38 | + body string |
| 39 | + expectedParams string |
| 40 | + headerKey string |
| 41 | + headerValue string |
| 42 | + }{ |
| 43 | + { |
| 44 | + http.MethodGet, |
| 45 | + "/foo", |
| 46 | + "", |
| 47 | + "", |
| 48 | + "Foo", |
| 49 | + "Bar", |
| 50 | + }, |
| 51 | + { |
| 52 | + http.MethodPost, |
| 53 | + "/foo/bar", |
| 54 | + "{\"foo\": \"bar\"}", |
| 55 | + "{\"foo\" => \"bar\"}", |
| 56 | + "Content-Type", |
| 57 | + "application/json", |
| 58 | + }, |
| 59 | + { |
| 60 | + http.MethodDelete, |
| 61 | + "/foo/1", |
| 62 | + "", |
| 63 | + "", |
| 64 | + "Bearer", |
| 65 | + "hello!", |
| 66 | + }, |
| 67 | + { |
| 68 | + http.MethodGet, |
| 69 | + "/foo/bar?hello=world", |
| 70 | + "", |
| 71 | + "{\"hello\" => \"world\"}", |
| 72 | + "Content-Type", |
| 73 | + "application/json!", |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + rpChannel := make(chan RequestPayload, len(testTable)) |
| 78 | + httpServer := Http{ResponseCode: 200, rendererChannels: []chan RequestPayload{rpChannel}} |
| 79 | + srv := httptest.NewServer(httpServer.routes()) |
| 80 | + defer srv.Close() |
| 81 | + |
| 82 | + for _, test := range testTable { |
| 83 | + b := bytes.NewBuffer([]byte(test.body)) |
| 84 | + req, err := http.NewRequest(test.method, srv.URL+test.path, b) |
| 85 | + |
| 86 | + if test.headerKey != "" { |
| 87 | + req.Header.Set(test.headerKey, test.headerValue) |
| 88 | + } |
| 89 | + |
| 90 | + if err != nil { |
| 91 | + t.Error(err) |
| 92 | + } |
| 93 | + |
| 94 | + resp, err := http.DefaultClient.Do(req) |
| 95 | + if err != nil { |
| 96 | + t.Error(err) |
| 97 | + } |
| 98 | + resp.Body.Close() |
| 99 | + |
| 100 | + rp := <-rpChannel |
| 101 | + |
| 102 | + if rp.Fields.Method != test.method { |
| 103 | + t.Errorf("Expected %s, got %s", test.method, rp.Fields.Method) |
| 104 | + } |
| 105 | + |
| 106 | + if rp.Fields.Url != test.path { |
| 107 | + t.Errorf("Expected %s, got %s", test.path, rp.Fields.Url) |
| 108 | + } |
| 109 | + |
| 110 | + if rp.Params != test.expectedParams { |
| 111 | + t.Errorf("Expected %s, got %s", test.expectedParams, rp.Params) |
| 112 | + } |
| 113 | + |
| 114 | + expectedHeaderValue := rp.Headers[test.headerKey][0] |
| 115 | + if expectedHeaderValue != test.headerValue { |
| 116 | + t.Errorf("Expected %s, got %s", expectedHeaderValue, test.headerValue) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestLogRequestManyRenderers(t *testing.T) { |
| 122 | + testTable := []struct { |
| 123 | + method string |
| 124 | + path string |
| 125 | + Body string |
| 126 | + expectedParams string |
| 127 | + }{ |
| 128 | + {http.MethodGet, "/foo", "", ""}, |
| 129 | + {http.MethodPost, "/foo/bar", "{\"foo\": \"bar\"}", "{\"foo\" => \"bar\"}"}, |
| 130 | + {http.MethodDelete, "/foo/1", "", ""}, |
| 131 | + {http.MethodGet, "/foo/bar?hello=world", "", "{\"hello\" => \"world\"}"}, |
| 132 | + } |
| 133 | + |
| 134 | + rpChannelA := make(chan RequestPayload, len(testTable)) |
| 135 | + rpChannelB := make(chan RequestPayload, len(testTable)) |
| 136 | + httpServer := Http{ |
| 137 | + ResponseCode: 200, |
| 138 | + rendererChannels: []chan RequestPayload{rpChannelA, rpChannelB}} |
| 139 | + srv := httptest.NewServer(httpServer.routes()) |
| 140 | + defer srv.Close() |
| 141 | + |
| 142 | + for _, test := range testTable { |
| 143 | + b := bytes.NewBuffer([]byte(test.Body)) |
| 144 | + req, err := http.NewRequest(test.method, srv.URL+test.path, b) |
| 145 | + |
| 146 | + if test.Body != "" { |
| 147 | + req.Header.Set("Content-Type", "application/json") |
| 148 | + } |
| 149 | + |
| 150 | + if err != nil { |
| 151 | + t.Error(err) |
| 152 | + } |
| 153 | + |
| 154 | + resp, err := http.DefaultClient.Do(req) |
| 155 | + if err != nil { |
| 156 | + t.Error(err) |
| 157 | + } |
| 158 | + resp.Body.Close() |
| 159 | + |
| 160 | + rpA := <-rpChannelA |
| 161 | + rpB := <-rpChannelB |
| 162 | + |
| 163 | + if rpA.Fields.Method != test.method { |
| 164 | + t.Errorf("Expected %s, got %s", test.method, rpA.Fields.Method) |
| 165 | + } |
| 166 | + |
| 167 | + if rpA.Fields.Url != test.path { |
| 168 | + t.Errorf("Expected %s, got %s", test.path, rpA.Fields.Url) |
| 169 | + } |
| 170 | + |
| 171 | + if rpA.Params != test.expectedParams { |
| 172 | + t.Errorf("Expected %s, got %s", test.expectedParams, rpA.Params) |
| 173 | + } |
| 174 | + |
| 175 | + if rpB.Fields.Method != test.method { |
| 176 | + t.Errorf("Expected %s, got %s", test.method, rpB.Fields.Method) |
| 177 | + } |
| 178 | + |
| 179 | + if rpB.Fields.Url != test.path { |
| 180 | + t.Errorf("Expected %s, got %s", test.path, rpB.Fields.Url) |
| 181 | + } |
| 182 | + |
| 183 | + if rpB.Params != test.expectedParams { |
| 184 | + t.Errorf("Expected %s, got %s", test.expectedParams, rpB.Params) |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments