-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
108 lines (88 loc) · 2.62 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"bytes"
"encoding/json"
"flag"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
"net/http"
"net/http/httptest"
"testing"
)
var (
container *Container
router *gin.Engine
)
func initContext() *cli.Context {
set := flag.NewFlagSet("test", 0)
set.String("mqtt-host", "tcp://localhost:1883", "doc")
set.String("mqtt-user", "", "doc")
set.String("mqtt-pass", "", "doc")
set.String("username", "user1", "doc")
set.String("password", "pass1", "doc")
set.String("port", "8080", "doc")
ctx := cli.NewContext(nil, set, nil)
return ctx
}
func init() {
ctx := initContext()
container = InitializeContainer(ctx)
router = SetupGin(container)
// Add Mock MQTT Client to Container
c := newMockClient()
c.Connect()
container.MqttClient = c
}
func TestStaticRoute(t *testing.T) {
t.Run("/Ping Static Router", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ping", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "pong", w.Body.String())
})
t.Run("/ Static Router", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "i am ready !", w.Body.String())
})
}
func TestPublishSuccess(t *testing.T) {
w := httptest.NewRecorder()
u := PublishDTO{Topic: "topic/test", Message: "t,1,1"}
req := makePublishRequest("user1", "pass1", u)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
// Convert the JSON response to a map
var response map[string]string
err := json.Unmarshal([]byte(w.Body.String()), &response)
value, exists := response["status"]
assert.Nil(t, err)
assert.True(t, exists)
assert.Equal(t, "ok", value)
}
func TestPublishFailureWithWrongCredentials(t *testing.T) {
w := httptest.NewRecorder()
u := PublishDTO{Topic: "topic/test", Message: "t,1,1"}
req := makePublishRequest("wronguser1", "wrongpass1", u)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusUnauthorized, w.Code)
}
func TestPublishFailureWithWrongPayload(t *testing.T) {
w := httptest.NewRecorder()
u := gin.H{}
req := makePublishRequest("user1", "pass1", u)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func makePublishRequest(username string, password string, payload interface{}) *http.Request {
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(payload)
req, _ := http.NewRequest("POST", "/publish", b)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.SetBasicAuth(username, password)
return req
}