summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/xenolf/lego/acme/http_test.go
blob: 33a48a331f6c1bc2aea94949ac5fb7fea4e03210 (plain)
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
package acme

import (
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

func TestHTTPHeadUserAgent(t *testing.T) {
	var ua, method string
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ua = r.Header.Get("User-Agent")
		method = r.Method
	}))
	defer ts.Close()

	_, err := httpHead(ts.URL)
	if err != nil {
		t.Fatal(err)
	}

	if method != "HEAD" {
		t.Errorf("Expected method to be HEAD, got %s", method)
	}
	if !strings.Contains(ua, ourUserAgent) {
		t.Errorf("Expected User-Agent to contain '%s', got: '%s'", ourUserAgent, ua)
	}
}

func TestHTTPGetUserAgent(t *testing.T) {
	var ua, method string
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ua = r.Header.Get("User-Agent")
		method = r.Method
	}))
	defer ts.Close()

	res, err := httpGet(ts.URL)
	if err != nil {
		t.Fatal(err)
	}
	res.Body.Close()

	if method != "GET" {
		t.Errorf("Expected method to be GET, got %s", method)
	}
	if !strings.Contains(ua, ourUserAgent) {
		t.Errorf("Expected User-Agent to contain '%s', got: '%s'", ourUserAgent, ua)
	}
}

func TestHTTPPostUserAgent(t *testing.T) {
	var ua, method string
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ua = r.Header.Get("User-Agent")
		method = r.Method
	}))
	defer ts.Close()

	res, err := httpPost(ts.URL, "text/plain", strings.NewReader("falalalala"))
	if err != nil {
		t.Fatal(err)
	}
	res.Body.Close()

	if method != "POST" {
		t.Errorf("Expected method to be POST, got %s", method)
	}
	if !strings.Contains(ua, ourUserAgent) {
		t.Errorf("Expected User-Agent to contain '%s', got: '%s'", ourUserAgent, ua)
	}
}

func TestUserAgent(t *testing.T) {
	ua := userAgent()

	if !strings.Contains(ua, defaultGoUserAgent) {
		t.Errorf("Expected UA to contain %s, got '%s'", defaultGoUserAgent, ua)
	}
	if !strings.Contains(ua, ourUserAgent) {
		t.Errorf("Expected UA to contain %s, got '%s'", ourUserAgent, ua)
	}
	if strings.HasSuffix(ua, " ") {
		t.Errorf("UA should not have trailing spaces; got '%s'", ua)
	}

	// customize the UA by appending a value
	UserAgent = "MyApp/1.2.3"
	ua = userAgent()
	if !strings.Contains(ua, defaultGoUserAgent) {
		t.Errorf("Expected UA to contain %s, got '%s'", defaultGoUserAgent, ua)
	}
	if !strings.Contains(ua, ourUserAgent) {
		t.Errorf("Expected UA to contain %s, got '%s'", ourUserAgent, ua)
	}
	if !strings.Contains(ua, UserAgent) {
		t.Errorf("Expected custom UA to contain %s, got '%s'", UserAgent, ua)
	}
}