-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_test.go
More file actions
90 lines (72 loc) · 1.88 KB
/
fetch_test.go
File metadata and controls
90 lines (72 loc) · 1.88 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
package fetch
import (
"fmt"
"net/http"
"testing"
)
type Example struct {
Hello string
}
var hosting bool
func host() {
if hosting == true {
return
}
http.HandleFunc("/plain", func(writer http.ResponseWriter, _ *http.Request) {
fmt.Fprintf(writer, "Hello World")
})
http.HandleFunc("/json", func(writer http.ResponseWriter, _ *http.Request) {
fmt.Fprintf(writer, "{ \"Hello\": \"World\" }\n")
})
hosting = true
http.ListenAndServe(":8080", nil)
}
func TestSimple(test *testing.T) {
go host()
response, err := Fetch[string]("http://localhost:8080/plain", Options[Empty]{})
test.Run("get response", func(test *testing.T) {
if err != nil {
test.Fatalf("HTTP response failed: %v\n", err)
} else {
test.Logf("HTTP response received\n")
}
})
test.Run("check response body", func(test *testing.T) {
if response.Body != "Hello World" {
test.Fatalf("HTTP response body expected \"Hello World\" but got \"%s\"", response.Body)
} else {
test.Logf("HTTP response was correct\n")
}
})
}
func TestAdvanced(test *testing.T) {
go host()
response, err := Fetch[Example]("http://localhost:8080/json", Options[string]{
Method: "POST",
Headers: Headers{
"User-Agent": "example",
"Example": []string{"Hello", "World"},
},
Body: "Hello World",
})
test.Run("get response", func(test *testing.T) {
if err != nil {
test.Fatalf("HTTP response failed: %v\n", err)
} else {
test.Logf("HTTP response received\n")
}
})
test.Run("check response body", func(test *testing.T) {
if response.Body.Hello != "World" {
test.Fatalf("HTTP response body expected { \"World\": \"World\" } but got %+v\n", response.Body)
} else {
test.Logf("HTTP response was correct\n")
}
})
}
func BenchmarkGet(benchmark *testing.B) {
go host()
for index := 0; index < benchmark.N; index++ {
Fetch[string]("http://localhost:8080/plain", Options[Empty]{})
}
}