| 1 | package lfshttp |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "strings" |
| 10 | "sync/atomic" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/stretchr/testify/assert" |
| 14 | "github.com/stretchr/testify/require" |
| 15 | ) |
| 16 | |
| 17 | type verboseTest struct { |
| 18 | Test string |
| 19 | } |
| 20 | |
| 21 | func TestVerboseEnabled(t *testing.T) { |
| 22 | var called uint32 |
| 23 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 24 | atomic.AddUint32(&called, 1) |
| 25 | t.Logf("srv req %s %s", r.Method, r.URL.Path) |
| 26 | assert.Equal(t, "POST", r.Method) |
| 27 | |
| 28 | assert.Equal(t, "Basic ABC", r.Header.Get("Authorization")) |
| 29 | body := &verboseTest{} |
| 30 | err := json.NewDecoder(r.Body).Decode(body) |
| 31 | assert.Nil(t, err) |
| 32 | assert.Equal(t, "Verbose", body.Test) |
| 33 | w.Header().Set("Content-Type", "application/json") |
| 34 | w.Write([]byte(`{"Status":"Ok"}`)) |
| 35 | })) |
| 36 | defer srv.Close() |
| 37 | |
| 38 | out := &bytes.Buffer{} |
| 39 | c := NewClient(nil) |
| 40 | c.Verbose = true |
| 41 | c.VerboseOut = out |
| 42 | |
| 43 | req, err := http.NewRequest("POST", srv.URL, nil) |
| 44 | req.Header.Set("Authorization", "Basic ABC") |
| 45 | req.Header.Set("Content-Type", "application/json") |
| 46 | require.Nil(t, err) |
| 47 | require.Nil(t, MarshalToRequest(req, verboseTest{"Verbose"})) |
| 48 | |
| 49 | res, err := c.Do(req) |
| 50 | require.Nil(t, err) |
| 51 | io.Copy(io.Discard, res.Body) |
| 52 | res.Body.Close() |
| 53 | |
| 54 | assert.Equal(t, 200, res.StatusCode) |
| 55 | assert.EqualValues(t, 1, called) |
| 56 | |
| 57 | s := out.String() |
| 58 | t.Log(s) |
| 59 | |
| 60 | expected := []string{ |
| 61 | "> Host: 127.0.0.1:", |
| 62 | "\n> Authorization: Basic * * * * *\n", |
| 63 | "\n> Content-Type: application/json\n", |
| 64 | "\n> Accept-Encoding: gzip\n", |
| 65 | "\n> \n" + `{"Test":"Verbose"}` + "\n\n", |
| 66 | |
| 67 | "\n< HTTP/1.1 200 OK\n", |
| 68 | "\n< Content-Type: application/json\n", |
| 69 | "\n< \n" + `{"Status":"Ok"}`, |
| 70 | } |
| 71 | |
| 72 | for _, substr := range expected { |
| 73 | if !assert.True(t, strings.Contains(s, substr)) { |
| 74 | t.Logf("missing: %q", substr) |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestVerboseWithBinaryBody(t *testing.T) { |
| 80 | var called uint32 |
| 81 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 82 | atomic.AddUint32(&called, 1) |
| 83 | t.Logf("srv req %s %s", r.Method, r.URL.Path) |
| 84 | assert.Equal(t, "POST", r.Method) |
| 85 | |
| 86 | assert.Equal(t, "Basic ABC", r.Header.Get("Authorization")) |
| 87 | by, err := io.ReadAll(r.Body) |
| 88 | assert.Nil(t, err) |
| 89 | assert.Equal(t, "binary-request", string(by)) |
| 90 | w.Header().Set("Content-Type", "application/octet-stream") |
| 91 | w.Write([]byte(`binary-response`)) |
| 92 | })) |
| 93 | defer srv.Close() |
| 94 | |
| 95 | out := &bytes.Buffer{} |
| 96 | c := NewClient(nil) |
| 97 | c.Verbose = true |
| 98 | c.VerboseOut = out |
| 99 | |
| 100 | buf := bytes.NewBufferString("binary-request") |
| 101 | req, err := http.NewRequest("POST", srv.URL, buf) |
| 102 | req.Header.Set("Authorization", "Basic ABC") |
| 103 | req.Header.Set("Content-Type", "application/octet-stream") |
| 104 | require.Nil(t, err) |
| 105 | |
| 106 | res, err := c.Do(req) |
| 107 | require.Nil(t, err) |
| 108 | io.Copy(io.Discard, res.Body) |
| 109 | res.Body.Close() |
| 110 | |
| 111 | assert.Equal(t, 200, res.StatusCode) |
| 112 | assert.EqualValues(t, 1, called) |
| 113 | |
| 114 | s := out.String() |
| 115 | t.Log(s) |
| 116 | |
| 117 | expected := []string{ |
| 118 | "> Host: 127.0.0.1:", |
| 119 | "\n> Authorization: Basic * * * * *\n", |
| 120 | "\n> Content-Type: application/octet-stream\n", |
| 121 | "\n> Accept-Encoding: gzip\n", |
| 122 | |
| 123 | "\n< HTTP/1.1 200 OK\n", |
| 124 | "\n< Content-Type: application/octet-stream\n", |
| 125 | } |
| 126 | |
| 127 | for _, substr := range expected { |
| 128 | if !assert.True(t, strings.Contains(s, substr)) { |
| 129 | t.Logf("missing: %q", substr) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | assert.False(t, strings.Contains(s, "binary"), "contains binary request or response body") |
| 134 | } |
| 135 | |
| 136 | func TestVerboseEnabledWithDebugging(t *testing.T) { |
| 137 | var called uint32 |
| 138 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 139 | atomic.AddUint32(&called, 1) |
| 140 | t.Logf("srv req %s %s", r.Method, r.URL.Path) |
| 141 | assert.Equal(t, "POST", r.Method) |
| 142 | |
| 143 | assert.Equal(t, "Basic ABC", r.Header.Get("Authorization")) |
| 144 | body := &verboseTest{} |
| 145 | err := json.NewDecoder(r.Body).Decode(body) |
| 146 | assert.Nil(t, err) |
| 147 | assert.Equal(t, "Verbose", body.Test) |
| 148 | w.Header().Set("Content-Type", "application/json") |
| 149 | w.Write([]byte(`{"Status":"Ok"}`)) |
| 150 | })) |
| 151 | defer srv.Close() |
| 152 | |
| 153 | out := &bytes.Buffer{} |
| 154 | c := NewClient(nil) |
| 155 | c.Verbose = true |
| 156 | c.VerboseOut = out |
| 157 | c.DebuggingVerbose = true |
| 158 | |
| 159 | req, err := http.NewRequest("POST", srv.URL, nil) |
| 160 | req.Header.Set("Authorization", "Basic ABC") |
| 161 | req.Header.Set("Content-Type", "application/json") |
| 162 | require.Nil(t, err) |
| 163 | require.Nil(t, MarshalToRequest(req, verboseTest{"Verbose"})) |
| 164 | |
| 165 | res, err := c.Do(req) |
| 166 | require.Nil(t, err) |
| 167 | io.Copy(io.Discard, res.Body) |
| 168 | res.Body.Close() |
| 169 | |
| 170 | assert.Equal(t, 200, res.StatusCode) |
| 171 | assert.EqualValues(t, 1, called) |
| 172 | |
| 173 | s := out.String() |
| 174 | t.Log(s) |
| 175 | |
| 176 | expected := []string{ |
| 177 | "> Host: 127.0.0.1:", |
| 178 | "\n> Authorization: Basic ABC\n", |
| 179 | "\n> Content-Type: application/json\n", |
| 180 | "\n> Accept-Encoding: gzip\n", |
| 181 | "\n> \n" + `{"Test":"Verbose"}` + "\n\n", |
| 182 | |
| 183 | "\n< HTTP/1.1 200 OK\n", |
| 184 | "\n< Content-Type: application/json\n", |
| 185 | "\n< \n" + `{"Status":"Ok"}`, |
| 186 | } |
| 187 | |
| 188 | for _, substr := range expected { |
| 189 | if !assert.True(t, strings.Contains(s, substr)) { |
| 190 | t.Logf("missing: %q", substr) |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | func TestVerboseDisabled(t *testing.T) { |
| 196 | var called uint32 |
| 197 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 198 | atomic.AddUint32(&called, 1) |
| 199 | t.Logf("srv req %s %s", r.Method, r.URL.Path) |
| 200 | assert.Equal(t, "POST", r.Method) |
| 201 | |
| 202 | assert.Equal(t, "Basic ABC", r.Header.Get("Authorization")) |
| 203 | body := &verboseTest{} |
| 204 | err := json.NewDecoder(r.Body).Decode(body) |
| 205 | assert.Nil(t, err) |
| 206 | assert.Equal(t, "Verbose", body.Test) |
| 207 | w.Header().Set("Content-Type", "application/json") |
| 208 | w.Write([]byte(`{"Status":"Ok"}`)) |
| 209 | })) |
| 210 | defer srv.Close() |
| 211 | |
| 212 | out := &bytes.Buffer{} |
| 213 | c := NewClient(nil) |
| 214 | c.Verbose = false |
| 215 | c.VerboseOut = out |
| 216 | c.DebuggingVerbose = true |
| 217 | |
| 218 | req, err := http.NewRequest("POST", srv.URL, nil) |
| 219 | req.Header.Set("Authorization", "Basic ABC") |
| 220 | req.Header.Set("Content-Type", "application/json") |
| 221 | require.Nil(t, err) |
| 222 | require.Nil(t, MarshalToRequest(req, verboseTest{"Verbose"})) |
| 223 | |
| 224 | res, err := c.Do(req) |
| 225 | require.Nil(t, err) |
| 226 | io.Copy(io.Discard, res.Body) |
| 227 | res.Body.Close() |
| 228 | |
| 229 | assert.Equal(t, 200, res.StatusCode) |
| 230 | assert.EqualValues(t, 1, called) |
| 231 | assert.EqualValues(t, 0, out.Len(), out.String()) |
| 232 | } |