forked from TheThingsNetwork/lorawan-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_test.go
More file actions
149 lines (121 loc) · 3.6 KB
/
web_test.go
File metadata and controls
149 lines (121 loc) · 3.6 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package web
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
echo "github.com/labstack/echo/v4"
"github.com/smartystreets/assertions"
"go.thethings.network/lorawan-stack/v3/pkg/random"
"go.thethings.network/lorawan-stack/v3/pkg/util/test"
"go.thethings.network/lorawan-stack/v3/pkg/util/test/assertions/should"
)
func handler(c echo.Context) error {
return nil
}
func TestGroup(t *testing.T) {
a := assertions.New(t)
s, err := New(test.Context())
if !a.So(err, should.BeNil) {
t.Fatal("Could not create a web instance")
}
a.So(s.echo, should.NotHaveRoute, "GET", "/")
s.GET("/", handler)
a.So(s.echo, should.HaveRoute, "GET", "/")
a.So(s.echo, should.NotHaveRoute, "POST", "/bar")
s.POST("/bar", handler)
a.So(s.echo, should.NotHaveRoute, "GET", "/bar")
a.So(s.echo, should.HaveRoute, "POST", "/bar")
grp := s.Group("/group")
grp.GET("/g", handler)
a.So(s.echo, should.HaveRoute, "GET", "/group/g")
ggrp := grp.Group("/quu")
ggrp.GET("/q", handler)
a.So(s.echo, should.HaveRoute, "GET", "/group/quu/q")
}
func TestIsZeros(t *testing.T) {
a := assertions.New(t)
{
res := isZeros([]byte{0, 0, 0, 0, 0})
a.So(res, should.BeTrue)
}
{
res := isZeros([]byte{0, 0, 0, 1, 0})
a.So(res, should.BeFalse)
}
}
func TestServeHTTP(t *testing.T) {
a := assertions.New(t)
s, err := New(test.Context())
if !a.So(err, should.BeNil) {
t.Fatal("Could not create a web instance")
}
// HTTP server returns 200 on valid route
{
req := httptest.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
s.GET("/", handler)
s.ServeHTTP(rec, req)
resp := rec.Result()
a.So(resp.StatusCode, should.Equal, http.StatusOK)
}
}
func TestStatic(t *testing.T) {
a := assertions.New(t)
s, err := New(test.Context())
if !a.So(err, should.BeNil) {
t.Fatal("Could not create a web instance")
}
dir, err := os.Getwd()
if !a.So(err, should.BeNil) {
t.Fatal("Could not create resolve testing directory")
}
s.Static("/assets", http.Dir(dir))
// HTTP server returns 200 on valid file request
{
req := httptest.NewRequest(echo.GET, "/assets/web_test.go", nil)
rec := httptest.NewRecorder()
s.ServeHTTP(rec, req)
resp := rec.Result()
body, _ := ioutil.ReadAll(resp.Body)
a.So(resp.StatusCode, should.Equal, http.StatusOK)
a.So(strings.HasPrefix(string(body), "//"), should.BeTrue)
}
// HTTP server returns 404 on invalid file request
{
req := httptest.NewRequest(echo.GET, "/assets/null.txt", nil)
rec := httptest.NewRecorder()
s.Static("/assets", http.Dir(dir+"/teststatic"))
s.ServeHTTP(rec, req)
resp := rec.Result()
a.So(resp.StatusCode, should.Equal, http.StatusNotFound)
}
}
func TestCookies(t *testing.T) {
a := assertions.New(t)
// Errors on illegal hash key byte size
{
_, err := New(test.Context(), WithCookieKeys(random.Bytes(2), nil))
a.So(err, should.NotBeNil)
}
// Errors on non 32bit block key
{
_, err := New(test.Context(), WithCookieKeys(nil, random.Bytes(31)))
a.So(err, should.NotBeNil)
}
}