|
| 1 | +package http_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log/slog" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/absmach/mproxy/pkg/session/mocks" |
| 11 | + |
| 12 | + mhttp "github.com/absmach/mproxy/pkg/http" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/mock" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + validUrl = "http://example.com" |
| 19 | + validAddess = "localhost:8080" |
| 20 | + valid = "valid" |
| 21 | + invalid = "invalid" |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + han *mocks.Handler |
| 26 | + log *slog.Logger |
| 27 | +) |
| 28 | + |
| 29 | +func newProxy(address, url string) (mhttp.Proxy, error) { |
| 30 | + han = new(mocks.Handler) |
| 31 | + log = new(slog.Logger) |
| 32 | + return mhttp.NewProxy(address, url, han, log) |
| 33 | +} |
| 34 | + |
| 35 | +func TestNewProxy(t *testing.T) { |
| 36 | + cases := []struct { |
| 37 | + desc string |
| 38 | + address string |
| 39 | + url string |
| 40 | + err error |
| 41 | + }{ |
| 42 | + { |
| 43 | + desc: "create proxy with valid", |
| 44 | + address: validAddess, |
| 45 | + url: validUrl, |
| 46 | + err: nil, |
| 47 | + }, |
| 48 | + { |
| 49 | + desc: "create proxy with invalid url", |
| 50 | + address: validAddess, |
| 51 | + url: "0000", |
| 52 | + err: nil, |
| 53 | + }, |
| 54 | + } |
| 55 | + for _, c := range cases { |
| 56 | + _, err := newProxy(c.address, c.url) |
| 57 | + assert.Equal(t, c.err, err, fmt.Sprintf("%s: expected %s got %s\n", c.desc, c.err, err)) |
| 58 | + |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestHandler(t *testing.T) { |
| 63 | + proxy, err := newProxy(validAddess, validUrl) |
| 64 | + assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err)) |
| 65 | + request := httptest.NewRequest(http.MethodGet, "http://example.com", nil) |
| 66 | + |
| 67 | + cases := []struct { |
| 68 | + desc string |
| 69 | + auth func() |
| 70 | + authConnectErr error |
| 71 | + authPublishErr error |
| 72 | + code int |
| 73 | + }{ |
| 74 | + { |
| 75 | + desc: "successful request with username and password and basic auth", |
| 76 | + auth: func() { |
| 77 | + request.SetBasicAuth("username", "password") |
| 78 | + }, |
| 79 | + code: http.StatusOK, |
| 80 | + }, |
| 81 | + { |
| 82 | + desc: "successful request with token", |
| 83 | + auth: func() { |
| 84 | + request.Header.Set("Authorization", valid) |
| 85 | + }, |
| 86 | + code: http.StatusOK, |
| 87 | + }, |
| 88 | + { |
| 89 | + desc: "request without authorization token", |
| 90 | + auth: func() { |
| 91 | + request.Header.Set("Authorization", "") |
| 92 | + }, |
| 93 | + code: http.StatusBadGateway, |
| 94 | + }, |
| 95 | + } |
| 96 | + for _, tc := range cases { |
| 97 | + tc.auth() |
| 98 | + responseRecorder := httptest.NewRecorder() |
| 99 | + sessionCall := han.On("AuthConnect", mock.Anything).Return(tc.authConnectErr) |
| 100 | + sessionCall1 := han.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(tc.authPublishErr) |
| 101 | + proxy.Handler(responseRecorder, request) |
| 102 | + assert.Equal(t, tc.code, responseRecorder.Code, fmt.Sprintf("%s: expected %d got %d\n", tc.desc, tc.code, responseRecorder.Code)) |
| 103 | + sessionCall.Unset() |
| 104 | + sessionCall1.Unset() |
| 105 | + |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestListen(t *testing.T) { |
| 110 | + proxy, err := newProxy(validAddess, validUrl) |
| 111 | + assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err)) |
| 112 | + |
| 113 | + t.Run("Listen", func(t *testing.T) { |
| 114 | + go func() { |
| 115 | + err := proxy.Listen() |
| 116 | + assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err)) |
| 117 | + }() |
| 118 | + }) |
| 119 | +} |
| 120 | + |
| 121 | +func TestListenTLS(t *testing.T) { |
| 122 | + proxy, err := newProxy(validAddess, validUrl) |
| 123 | + assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err)) |
| 124 | + |
| 125 | + t.Run("ListenTLS", func(t *testing.T) { |
| 126 | + go func() { |
| 127 | + err := proxy.ListenTLS("cert", "key") |
| 128 | + assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err)) |
| 129 | + }() |
| 130 | + }) |
| 131 | +} |
0 commit comments