-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseurl_test.go
69 lines (56 loc) · 1.17 KB
/
baseurl_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
package baseurl
import (
"crypto/tls"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
var testHTTPHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, BaseUrl(r)) })
func TestHTTP(t *testing.T) {
ts := httptest.NewServer(testHTTPHandler)
defer ts.Close()
res, err := http.Get(ts.URL)
if err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if string(data) != ts.URL {
t.Fatalf(`failed to get BaseUrl:
expect: %v
got: %v`, ts.URL, string(data))
}
}
func TestHTTPS(t *testing.T) {
ts := httptest.NewTLSServer(testHTTPHandler)
defer ts.Close()
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := &http.Client{Transport: tr}
res, err := client.Get(ts.URL)
if err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if string(data) != ts.URL {
t.Fatalf(`failed to get BaseUrl:
expect: %v
got: %v`, ts.URL, string(data))
}
if !strings.HasPrefix(string(data), `https://`) {
t.Fatal(`this url is not https://`)
}
}