forked from adamveld12/gittp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
context_test.go
74 lines (65 loc) · 2.71 KB
/
context_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
package gittp
import (
"bytes"
"net/http"
"testing"
)
// func Test_newHandlerContext(t *testing.T) {
// testCases := []*http.Request{
// createRequest("POST", "/info/refs?service=git-receive-pack"),
// createRequest("GET", "/info/refs?service=git-receive-pack"),
// createRequest("POST", "/git-receive-pack"),
// createRequest("GET", "/git-receive-pack"),
// createRequest("POST", "/git-upload-pack"),
// createRequest("GET", "/git-upload-pack"),
// }
// }
func createRequest(method, url string) (req *http.Request) {
req, _ = http.NewRequest(method, url, &bytes.Buffer{})
return
}
func Test_contentType(t *testing.T) {
cases := []struct {
isAdvertisement bool
serviceType string
expected string
}{
{true, "git-receive-pack", "application/x-git-receive-pack-advertisement"},
{false, "git-receive-pack", "application/x-git-receive-pack-result"},
{false, "git-upload-pack", "application/x-git-upload-pack-result"},
{true, "git-upload-pack", "application/x-git-upload-pack-advertisement"},
}
for _, c := range cases {
actual := contentType(c.serviceType, c.isAdvertisement)
if actual != c.expected {
t.Errorf("expected %v - actual %v", c.expected, actual)
}
}
}
func Test_detectServiceType(t *testing.T) {
testCases := map[string]string{
"/info/refs?service=git-receive-pack": "git-receive-pack",
"/info/refs?service=git-upload-pack": "git-upload-pack",
"/info/refs": errNoMatchingService.Error(),
"adam/testrepo.git/info/refs?service=git-receive-pack": "git-receive-pack",
"adam/test/repo/info/refs/info/refs?service=git-upload-pack": "git-upload-pack",
"/git-receive-pack": "git-receive-pack",
"/git-upload-pack": "git-upload-pack",
"adam/test.git/git-receive-pack": "git-receive-pack",
"adam/test.git/git-upload-pack": "git-upload-pack",
"git-upload-pack/git-receive-pack": "git-receive-pack",
"git-receive-pack/git-upload-pack": "git-upload-pack",
"adam/test/git-upload-pack/git-receive-pack": "git-receive-pack",
"adam/test/git": errNoMatchingService.Error(),
}
for rawURL, expected := range testCases {
actual, err := detectServiceType(parseURL(rawURL))
if err != nil && err.Error() != expected {
t.Logf("testing %s -> %s\n", rawURL, expected)
t.Errorf("expected:\n%s\nactual:\n%s\n", expected, err.Error())
} else if err == nil && actual != expected {
t.Logf("testing %s -> %s\n", rawURL, expected)
t.Errorf("expected:\n%s\nactual:\n%s\n", expected, actual)
}
}
}