-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
130 lines (111 loc) · 2.9 KB
/
main_test.go
File metadata and controls
130 lines (111 loc) · 2.9 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
externalnode "github.com/ProxeusApp/node-go"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
)
func TestNode(t *testing.T) {
e := echo.New()
// test core
testCore := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
assert.Nil(t, err, "Cannot read body")
r.Body.Close()
n := externalnode.ExternalNode{}
err = json.Unmarshal(body, &n)
assert.Nil(t, err, "Cannot unmarshal body")
assert.EqualValues(t, "http://example.com", n.Url, "Wrong service url")
assert.EqualValues(t, "service", n.Name, "Wrong service name")
assert.EqualValues(t, "service", n.ID, "Wrong service name")
assert.EqualValues(t, "topsecret", n.Secret, "Wrong secret")
assert.EqualValues(t, serviceDetail, n.Detail, "Wrong service detail")
}))
defer testCore.Close()
// test target
testTarget := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
assert.Nil(t, err, "Cannot read body")
r.Body.Close()
content := map[string]string{}
err = json.Unmarshal(body, &content)
assert.Nil(t, err, "Cannot unmarshal body")
assert.EqualValues(t, "Hello", content["test1"], "Wrong content")
io.WriteString(w, string(body))
}))
defer testTarget.Close()
h := &handler{
proxeusURL: testCore.URL,
serviceName: "service",
serviceUrl: "http://example.com",
jwtSecret: "topsecret",
targetURL: testTarget.URL,
headers: [][]string{
{
"foo", "bar",
},
},
}
err := h.register()
if err != nil {
t.Errorf("Expected not error but got %s", err.Error())
}
req := httptest.NewRequest(http.MethodPost, "/next", strings.NewReader(`{"test1":"Hello"}`))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
if assert.NoError(t, h.next(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
result, err := ioutil.ReadAll(rec.Body)
assert.Nil(t, err, "Cannot read result body")
assert.Equal(t, `{"test1":"Hello"}`, string(result))
}
}
func TestExtractHeaders(t *testing.T) {
tests := []struct {
title string
env []string
headers [][]string
}{
{
title: "nil",
},
{
title: "none",
env: []string{
"FOOBAR=foobar",
"",
"BAR=foo",
},
},
{
title: "mixed",
env: []string{
"FOOBAR=foobar",
"",
"JSON_SENDER_HEADER_foobar=ABC123",
"BAR=foo",
"JSON_SENDER_HEADER_barfoo=321CBA",
},
headers: [][]string{
{"foobar", "ABC123"},
{"barfoo", "321CBA"},
},
},
}
for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
headers := extractHeaders(test.env)
if !reflect.DeepEqual(headers, test.headers) {
t.Errorf("Expected %v but got %v", test.headers, headers)
}
})
}
}