66 lines
2.0 KiB
Go
66 lines
2.0 KiB
Go
package app
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseGitPath(t *testing.T) {
|
|
owner, repo, rest, ok := parseGitPath("/Alice/Demo.git/info/refs")
|
|
if !ok {
|
|
t.Fatal("expected path to parse")
|
|
}
|
|
if owner != "Alice" || repo != "Demo" || rest != "/info/refs" {
|
|
t.Fatalf("unexpected parse: %s %s %s", owner, repo, rest)
|
|
}
|
|
if _, _, _, ok := parseGitPath("/bad/path"); ok {
|
|
t.Fatal("expected invalid path")
|
|
}
|
|
if _, _, _, ok := parseGitPath("/too/many/parts.git/info/refs"); ok {
|
|
t.Fatal("expected invalid owner/repo path")
|
|
}
|
|
}
|
|
|
|
func TestGitService(t *testing.T) {
|
|
req := &http.Request{Method: http.MethodGet, URL: &url.URL{RawQuery: "service=git-upload-pack"}}
|
|
if got := gitService(req); got != "git-upload-pack" {
|
|
t.Fatalf("unexpected upload-pack service: %q", got)
|
|
}
|
|
req = &http.Request{Method: http.MethodPost, URL: &url.URL{Path: "/alice/demo.git/git-receive-pack"}}
|
|
if got := gitService(req); got != "git-receive-pack" {
|
|
t.Fatalf("unexpected receive-pack service: %q", got)
|
|
}
|
|
req = &http.Request{Method: http.MethodGet, URL: &url.URL{Path: "/alice/demo.git/info/refs"}}
|
|
if got := gitService(req); got != "git-upload-pack" {
|
|
t.Fatalf("default service = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestWriteCGIResponse(t *testing.T) {
|
|
rr := httptest.NewRecorder()
|
|
writeCGIResponse(rr, []byte("Status: 201 Created\r\nContent-Type: text/plain\r\nX-Test: one\r\n\r\nbody"))
|
|
if rr.Code != http.StatusCreated {
|
|
t.Fatalf("status = %d", rr.Code)
|
|
}
|
|
if rr.Header().Get("Content-Type") != "text/plain" || rr.Header().Get("X-Test") != "one" {
|
|
t.Fatalf("unexpected headers: %#v", rr.Header())
|
|
}
|
|
if rr.Body.String() != "body" {
|
|
t.Fatalf("body = %q", rr.Body.String())
|
|
}
|
|
|
|
rr = httptest.NewRecorder()
|
|
writeCGIResponse(rr, []byte("Content-Type: text/plain\n\nplain"))
|
|
if rr.Code != http.StatusOK || rr.Body.String() != "plain" {
|
|
t.Fatalf("status/body = %d/%q", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
rr = httptest.NewRecorder()
|
|
writeCGIResponse(rr, []byte("raw"))
|
|
if rr.Body.String() != "raw" {
|
|
t.Fatalf("raw body = %q", rr.Body.String())
|
|
}
|
|
}
|