-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp_test.go
112 lines (89 loc) · 2.38 KB
/
http_test.go
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
package main
import (
"bytes"
"fmt"
"net/http/httptest"
"net/url"
"testing"
"github.com/gofiber/fiber/v2"
)
func TestHTTPBasicAuthFailed(t *testing.T) {
app, _ := HTTPServer("/", "user", "pass")
req := httptest.NewRequest("GET", "/", nil)
resp, _ := app.Test(req)
if resp.StatusCode != 401 {
t.Errorf("Status must be 403")
}
}
func TestHTTPSuccessTest(t *testing.T) {
app, _ := HTTPServer("/", "user", "pass")
req1 := httptest.NewRequest("GET", "/not-found", nil)
req1.SetBasicAuth("user", "pass")
resp1, _ := app.Test(req1)
if resp1.StatusCode != 404 {
t.Errorf("Status must be 404")
}
req2 := httptest.NewRequest("GET", "/whois/localhost", nil)
req2.SetBasicAuth("user", "pass")
resp2, _ := app.Test(req2)
if resp2.StatusCode != 400 {
t.Errorf("Status must be 400")
}
req5 := httptest.NewRequest("GET", "/validate/nic.not-exist", nil)
req5.SetBasicAuth("user", "pass")
resp5, _ := app.Test(req5)
if resp5.StatusCode != 400 {
t.Errorf("Status must be 400")
}
}
func TestHTTPSuccessTest2(t *testing.T) {
skipCI(t)
app, _ := HTTPServer("/", "user", "pass")
domains := []string{"google.com", "wikipedia.org", "nic.ir", "ایرنیک.ایران"}
for _, domain := range domains {
req3 := httptest.NewRequest("GET", "/whois/"+url.QueryEscape(domain), nil)
req3.SetBasicAuth("user", "pass")
resp3, err3 := app.Test(req3)
fmt.Println("====")
fmt.Println(domain)
if err3 != nil {
fmt.Println(err3)
} else {
buf := new(bytes.Buffer)
buf.ReadFrom(resp3.Body)
body := buf.String()
fmt.Println(body)
}
}
}
func TestHTTPBasicAuthSuccess(t *testing.T) {
app, api := HTTPServer("/base", "user", "pass")
api.Get("/ok", func(c *fiber.Ctx) error {
return c.SendString("OK")
})
api.Get("/error", func(c *fiber.Ctx) error {
return fiber.ErrInternalServerError
})
req := httptest.NewRequest("GET", "/base/ok", nil)
req.SetBasicAuth("user", "pass")
resp, _ := app.Test(req)
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
body := buf.String()
if resp.StatusCode != 200 {
fmt.Println(resp.StatusCode)
t.Errorf("Status must be 200")
}
if body != "OK" {
fmt.Println(resp.StatusCode)
t.Errorf("Status must be 200")
}
req = httptest.NewRequest("GET", "/base/error", nil)
req.SetBasicAuth("user", "pass")
resp, _ = app.Test(req)
if resp.StatusCode != 500 {
fmt.Println(resp.StatusCode)
t.Errorf("Status must be 500")
}
return
}