Skip to content

Commit 9bf6172

Browse files
committed
feat:Add tests to mproxy
Signed-off-by: felix.gateru <felix.gateru@gmail.com>
1 parent 695f58d commit 9bf6172

File tree

6 files changed

+303
-1
lines changed

6 files changed

+303
-1
lines changed

go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,11 @@ require (
1111
golang.org/x/sync v0.6.0
1212
)
1313

14-
require golang.org/x/net v0.20.0 // indirect
14+
require (
15+
github.com/davecgh/go-spew v1.1.1 // indirect
16+
github.com/pmezard/go-difflib v1.0.0 // indirect
17+
github.com/stretchr/objx v0.5.0 // indirect
18+
github.com/stretchr/testify v1.8.4 // indirect
19+
golang.org/x/net v0.20.0 // indirect
20+
gopkg.in/yaml.v3 v3.0.1 // indirect
21+
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14
github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik=
25
github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE=
36
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
47
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
58
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
69
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
10+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
11+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
12+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
13+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
14+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
15+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
16+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
17+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
18+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
19+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
720
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
821
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
922
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
1023
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
24+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
25+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
26+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
27+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/http/http_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
)
21+
22+
var (
23+
han *mocks.Handler
24+
log *slog.Logger
25+
)
26+
27+
func newProxy(address, url string) (mhttp.Proxy, error) {
28+
han = new(mocks.Handler)
29+
log = new(slog.Logger)
30+
return mhttp.NewProxy(address, url, han, log)
31+
}
32+
33+
func TestNewProxy(t *testing.T) {
34+
cases := []struct {
35+
desc string
36+
address string
37+
url string
38+
err error
39+
}{
40+
{
41+
desc: "create proxy with valid",
42+
address: validAddess,
43+
url: validUrl,
44+
err: nil,
45+
},
46+
{
47+
desc: "create proxy with invalid url",
48+
address: validAddess,
49+
url: "0000",
50+
err: nil,
51+
},
52+
}
53+
for _, c := range cases {
54+
_, err := newProxy(c.address, c.url)
55+
assert.Equal(t, c.err, err, fmt.Sprintf("%s: expected %s got %s\n", c.desc, c.err, err))
56+
57+
}
58+
}
59+
60+
func TestHandler(t *testing.T) {
61+
proxy, err := newProxy(validAddess, validUrl)
62+
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
63+
64+
t.Run("Successful Request", func(t *testing.T) {
65+
request := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
66+
request.SetBasicAuth("username", "password")
67+
responseRecorder := httptest.NewRecorder()
68+
69+
han.On("AuthConnect", mock.Anything).Return(nil)
70+
han.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil)
71+
proxy.Handler(responseRecorder, request)
72+
73+
assert.Equal(t, http.StatusOK, responseRecorder.Code, "Handler should return status OK for successful request")
74+
})
75+
}
76+
77+
func TestListen(t *testing.T) {
78+
proxy, err := newProxy(validAddess, validUrl)
79+
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
80+
81+
t.Run("Listen", func(t *testing.T) {
82+
go func() {
83+
err := proxy.Listen()
84+
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
85+
}()
86+
})
87+
}
88+
89+
// func TestListenTLS(t *testing.T) {
90+
// proxy, err := newProxy(validAddess, validUrl)
91+
// assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
92+
93+
94+
// t.Run("ListenTLS", func(t *testing.T) {
95+
// go func() {
96+
// err := proxy.ListenTLS("cert", "key")
97+
// assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
98+
// }()
99+
// })
100+
// }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package websocket_test
2+

pkg/session/handler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package session
33
import "context"
44

55
// Handler is an interface for mProxy hooks
6+
7+
//go:generate mockery --name Handler --filename handler.go --quiet --note "Copyright (c) Abstract Machines"
68
type Handler interface {
79
// Authorization on client `CONNECT`
810
// Each of the params are passed by reference, so that it can be changed

pkg/session/mocks/handler.go

Lines changed: 174 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)