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

Function TestHTTP

pkg/fetch/http_test.go:28–72  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

26)
27
28func TestHTTP(t *testing.T) {
29 a := assertions.New(t)
30
31 // Invalid path
32 {
33 fetcher, err := fetch.FromHTTP(http.DefaultClient, "")
34 if a.So(err, should.BeNil) && a.So(fetcher, should.NotBeNil) {
35 _, err = fetcher.File("test")
36 a.So(err, should.BeError)
37 }
38
39 fetcher, err = fetch.FromHTTP(http.DefaultClient, "/asd")
40 if a.So(err, should.NotBeNil) {
41 a.So(fetcher, should.BeNil)
42 }
43 }
44
45 httpmock.Activate()
46 defer httpmock.DeactivateAndReset()
47
48 serverHost := "http://server"
49 content := "server content"
50
51 httpmock.RegisterResponder("GET", fmt.Sprintf("%s/success", serverHost), httpmock.NewStringResponder(200, content))
52 httpmock.RegisterResponder("GET", fmt.Sprintf("%s/fail", serverHost), httpmock.NewStringResponder(500, ""))
53
54 fetcher, err := fetch.FromHTTP(http.DefaultClient, serverHost)
55 if !a.So(err, should.BeNil) {
56 t.Fatalf("Failed to construct HTTP fetcher: %v", err)
57 }
58
59 // Valid response code
60 {
61 receivedContent, err := fetcher.File("success")
62 if a.So(err, should.BeNil) {
63 a.So(string(receivedContent), should.Equal, content)
64 }
65 }
66
67 // Internal error response code
68 {
69 _, err := fetcher.File("fail")
70 a.So(err, should.NotBeNil)
71 }
72}

Callers

nothing calls this directly

Calls 5

FromHTTPFunction · 0.92
ActivateMethod · 0.80
NewMethod · 0.65
FileMethod · 0.65
FatalfMethod · 0.65

Tested by

no test coverage detected