forked from TheThingsNetwork/lorawan-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_test.go
More file actions
119 lines (100 loc) · 3.28 KB
/
http_test.go
File metadata and controls
119 lines (100 loc) · 3.28 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
// 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 fetch_test
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/smartystreets/assertions"
"go.thethings.network/lorawan-stack/v3/pkg/fetch"
"go.thethings.network/lorawan-stack/v3/pkg/util/test"
"go.thethings.network/lorawan-stack/v3/pkg/util/test/assertions/should"
)
func TestHTTP(t *testing.T) {
a := assertions.New(t)
// Invalid path
{
fetcher, err := fetch.FromHTTP("", false)
if a.So(err, should.BeNil) && a.So(fetcher, should.NotBeNil) {
_, err = fetcher.File("test")
a.So(err, should.BeError)
}
fetcher, err = fetch.FromHTTP("/asd", false)
if a.So(err, should.NotBeNil) {
a.So(fetcher, should.BeNil)
}
}
httpmock.Activate()
defer httpmock.DeactivateAndReset()
serverHost := "http://server"
content := "server content"
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/success", serverHost), httpmock.NewStringResponder(200, content))
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/fail", serverHost), httpmock.NewStringResponder(500, ""))
fetcher, err := fetch.FromHTTP(serverHost, false)
if !a.So(err, should.BeNil) {
t.Fatalf("Failed to construct HTTP fetcher: %v", err)
}
// Valid response code
{
receivedContent, err := fetcher.File("success")
if a.So(err, should.BeNil) {
a.So(string(receivedContent), should.Equal, content)
}
}
// Internal error response code
{
_, err := fetcher.File("fail")
a.So(err, should.NotBeNil)
}
}
func TestHTTPCache(t *testing.T) {
a := assertions.New(t)
cachedContent := "cached"
nonCachedContent := "non-cached"
servedContent := cachedContent
s := &http.Server{
Addr: "localhost:8083",
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
now := time.Now()
w.Header().Add("Date", now.Format(time.RFC1123))
w.Header().Add("Expires", now.Add(5*time.Minute).Format(time.RFC1123))
_, err := w.Write([]byte(servedContent))
if err != nil {
w.WriteHeader(500)
}
return
}),
ReadTimeout: time.Second,
WriteTimeout: time.Second,
}
go s.ListenAndServe()
defer s.Close()
time.Sleep(10 * test.Delay)
fetcher, err := fetch.FromHTTP(fmt.Sprintf("http://%s", s.Addr), true)
if !a.So(err, should.BeNil) {
t.Fatalf("Failed to construct HTTP fetcher: %v", err)
}
receivedContent, err := fetcher.File("cached")
a.So(err, should.BeNil)
a.So(string(receivedContent), should.Equal, cachedContent)
servedContent = nonCachedContent
receivedContent, err = fetcher.File("cached")
a.So(err, should.BeNil)
a.So(string(receivedContent), should.Equal, cachedContent)
receivedContent, err = fetcher.File("noncached")
a.So(err, should.BeNil)
a.So(string(receivedContent), should.Equal, nonCachedContent)
}