MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / TestHTTP

Function TestHTTP

pkg/component/component_test.go:75–180  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

73}
74
75func TestHTTP(t *testing.T) {
76 a := assertions.New(t)
77
78 httpAddress, httpsAddress := "0.0.0.0:9185", "0.0.0.0:9186"
79 baseConfig := component.Config{}
80
81 workingRoutePath := "/ok"
82 workingRoute := registererFunc(func(s *web.Server) {
83 s.Router().Path(workingRoutePath).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
84 w.WriteHeader(http.StatusOK)
85 })
86 })
87
88 // HTTP
89 {
90 config := baseConfig
91 config.HTTP.Listen = httpAddress
92 config.HTTP.ListenTLS = ""
93
94 c, err := component.New(test.GetLogger(t), &config)
95 a.So(err, should.BeNil)
96 c.RegisterWeb(workingRoute)
97
98 err = c.Start()
99 a.So(err, should.BeNil)
100
101 {
102 // Non-registered path
103 resp, err := http.Get(fmt.Sprintf("http://%s/not found", httpAddress))
104 a.So(err, should.BeNil)
105 a.So(resp.StatusCode, should.Equal, http.StatusNotFound)
106
107 // Registered path
108 resp, err = http.Get(fmt.Sprintf("http://%s%s", httpAddress, workingRoutePath))
109 a.So(err, should.BeNil)
110 a.So(resp.StatusCode, should.Equal, http.StatusOK)
111 }
112
113 c.Close()
114 }
115
116 // Invalid HTTP port
117 {
118 config := baseConfig
119 config.HTTP.Listen = "0.0.0.0:12391483"
120
121 c, err := component.New(test.GetLogger(t), &config)
122 a.So(err, should.BeNil)
123
124 err = c.Start()
125 a.So(err, should.NotBeNil)
126 }
127
128 // HTTPS
129 {
130 config := baseConfig
131
132 config.HTTP.Listen = ""

Callers

nothing calls this directly

Calls 10

NewFunction · 0.92
GetLoggerFunction · 0.92
registererFuncFuncType · 0.85
RouterMethod · 0.80
RegisterWebMethod · 0.80
NewMethod · 0.65
GetMethod · 0.65
CloseMethod · 0.65
ReadFileMethod · 0.65
StartMethod · 0.45

Tested by

no test coverage detected