|
| 1 | +package proxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/go-faker/faker/v4" |
| 14 | + "github.com/go-http-utils/headers" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + |
| 18 | + "github.com/ARM-software/golang-utils/utils/commonerrors" |
| 19 | + "github.com/ARM-software/golang-utils/utils/commonerrors/errortest" |
| 20 | + "github.com/ARM-software/golang-utils/utils/safecast" |
| 21 | + "github.com/ARM-software/golang-utils/utils/safeio" |
| 22 | +) |
| 23 | + |
| 24 | +func TestProxy(t *testing.T) { |
| 25 | + content := faker.Paragraph() |
| 26 | + path := faker.URL() |
| 27 | + password := faker.Password() |
| 28 | + tests := []struct { |
| 29 | + request *http.Request |
| 30 | + }{ |
| 31 | + { |
| 32 | + request: httptest.NewRequest(http.MethodGet, faker.URL(), io.NopCloser(strings.NewReader(content))), |
| 33 | + }, |
| 34 | + { |
| 35 | + request: httptest.NewRequest(http.MethodGet, faker.URL(), strings.NewReader(content)), |
| 36 | + }, |
| 37 | + { |
| 38 | + request: httptest.NewRequest(http.MethodGet, faker.URL(), io.NopCloser(bytes.NewReader([]byte(content)))), |
| 39 | + }, |
| 40 | + { |
| 41 | + request: httptest.NewRequest(http.MethodGet, faker.URL(), bytes.NewReader([]byte(content))), |
| 42 | + }, |
| 43 | + { |
| 44 | + request: httptest.NewRequest(http.MethodGet, faker.URL(), io.NopCloser(bytes.NewBuffer([]byte(content)))), |
| 45 | + }, |
| 46 | + { |
| 47 | + request: httptest.NewRequest(http.MethodGet, faker.URL(), bytes.NewBuffer([]byte(content))), |
| 48 | + }, |
| 49 | + } |
| 50 | + for i := range tests { |
| 51 | + test := tests[i] |
| 52 | + t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 53 | + req := test.request |
| 54 | + req.Header.Set(headers.AccessControlAllowOrigin, faker.Word()) |
| 55 | + req.Header.Set(headers.XHTTPMethodOverride, http.MethodPut) |
| 56 | + req.Header.Set(headers.Authorization, password) |
| 57 | + assert.NotEqual(t, req.URL.String(), path) |
| 58 | + _, err := ProxyRequest(nil, http.MethodPost, "/") |
| 59 | + errortest.AssertError(t, err, commonerrors.ErrUndefined) |
| 60 | + preq, err := ProxyRequest(req, " ", path) |
| 61 | + require.NoError(t, err) |
| 62 | + require.NotNil(t, preq) |
| 63 | + assert.Equal(t, path, preq.URL.String()) |
| 64 | + assert.Equal(t, http.MethodGet, preq.Method) |
| 65 | + assert.NotEmpty(t, preq.Header.Get(headers.AccessControlAllowOrigin)) |
| 66 | + assert.NotEmpty(t, preq.Header.Get(headers.Authorization)) |
| 67 | + assert.NotZero(t, preq.ContentLength) |
| 68 | + resp := generateTestResponseBasedOnRequest(t, preq) |
| 69 | + defer func() { |
| 70 | + if resp != nil { |
| 71 | + _ = resp.Body.Close() |
| 72 | + } |
| 73 | + }() |
| 74 | + w := httptest.NewRecorder() |
| 75 | + require.NoError(t, ProxyResponse(context.Background(), resp, w)) |
| 76 | + proxiedResp := w.Result() |
| 77 | + defer func() { _ = proxiedResp.Body.Close() }() |
| 78 | + assert.Empty(t, w.Header().Get(headers.AccessControlAllowOrigin)) |
| 79 | + assert.Equal(t, http.MethodPut, w.Header().Get(headers.XHTTPMethodOverride)) |
| 80 | + assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 81 | + responseContent, err := safeio.ReadAll(context.Background(), proxiedResp.Body) |
| 82 | + require.NoError(t, err) |
| 83 | + assert.Equal(t, content, string(responseContent)) |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestEmptyResponse(t *testing.T) { |
| 89 | + path := faker.URL() |
| 90 | + tests := []struct { |
| 91 | + request *http.Request |
| 92 | + }{ |
| 93 | + { |
| 94 | + httptest.NewRequest(http.MethodGet, faker.URL(), nil), |
| 95 | + }, |
| 96 | + { |
| 97 | + request: httptest.NewRequest(http.MethodGet, faker.URL(), http.NoBody), |
| 98 | + }, |
| 99 | + { |
| 100 | + request: httptest.NewRequest(http.MethodGet, faker.URL(), io.NopCloser(http.NoBody)), |
| 101 | + }, |
| 102 | + { |
| 103 | + request: httptest.NewRequest(http.MethodGet, faker.URL(), bytes.NewReader(nil)), |
| 104 | + }, |
| 105 | + { |
| 106 | + request: httptest.NewRequest(http.MethodGet, faker.URL(), io.NopCloser(bytes.NewBuffer(nil))), |
| 107 | + }, |
| 108 | + { |
| 109 | + request: httptest.NewRequest(http.MethodGet, faker.URL(), strings.NewReader("")), |
| 110 | + }, |
| 111 | + { |
| 112 | + request: httptest.NewRequest(http.MethodGet, faker.URL(), io.NopCloser(strings.NewReader(""))), |
| 113 | + }, |
| 114 | + } |
| 115 | + for i := range tests { |
| 116 | + test := tests[i] |
| 117 | + t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 118 | + req := test.request |
| 119 | + assert.NotEqual(t, req.URL.String(), path) |
| 120 | + preq, err := ProxyRequest(req, http.MethodPost, path) |
| 121 | + require.NoError(t, err) |
| 122 | + require.NotNil(t, preq) |
| 123 | + assert.Equal(t, path, preq.URL.String()) |
| 124 | + assert.Equal(t, http.MethodPost, preq.Method) |
| 125 | + assert.Zero(t, preq.ContentLength) |
| 126 | + |
| 127 | + resp := generateTestResponseBasedOnRequest(t, preq) |
| 128 | + defer func() { |
| 129 | + if resp != nil { |
| 130 | + _ = resp.Body.Close() |
| 131 | + } |
| 132 | + }() |
| 133 | + w := httptest.NewRecorder() |
| 134 | + require.NoError(t, ProxyResponse(context.Background(), resp, w)) |
| 135 | + require.NoError(t, err) |
| 136 | + returnedResp := w.Result() |
| 137 | + assert.LessOrEqual(t, returnedResp.ContentLength, safecast.ToInt64(0)) |
| 138 | + assert.Equal(t, http.StatusOK, returnedResp.StatusCode) |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func loopTestHandler(t *testing.T, w http.ResponseWriter, r *http.Request) { |
| 144 | + t.Helper() |
| 145 | + require.NotNil(t, r) |
| 146 | + require.NotNil(t, w) |
| 147 | + for k, v := range r.Header { |
| 148 | + for h := range v { |
| 149 | + w.Header().Add(k, v[h]) |
| 150 | + } |
| 151 | + } |
| 152 | + written, err := safeio.CopyDataWithContext(r.Context(), r.Body, w) |
| 153 | + require.NoError(t, err) |
| 154 | + w.Header().Add(headers.ContentLength, strconv.FormatInt(written, 10)) |
| 155 | + w.WriteHeader(http.StatusOK) |
| 156 | +} |
| 157 | + |
| 158 | +func generateTestResponseBasedOnRequest(t *testing.T, r *http.Request) *http.Response { |
| 159 | + t.Helper() |
| 160 | + require.NotNil(t, r) |
| 161 | + w := httptest.NewRecorder() |
| 162 | + loopTestHandler(t, w, r) |
| 163 | + return w.Result() |
| 164 | +} |
0 commit comments