diff --git a/githttptransfer/githttptransfer_test.go b/githttptransfer/githttptransfer_test.go index 9e5a83b..7a7a524 100644 --- a/githttptransfer/githttptransfer_test.go +++ b/githttptransfer/githttptransfer_test.go @@ -8,105 +8,69 @@ import ( "testing" ) -// TODO Could be converted to a table driven test with the next test -// https://github.com/golang/go/wiki/TableDrivenTests -func Test_it_should_return_403_if_upload_pack_is_off(t *testing.T) { +func Test_GitHTTPTransfer_GitHTTPTransferOption(t *testing.T) { if _, err := exec.LookPath("git"); err != nil { log.Println("git is not found. so skip test.") } - ght, err := New("/data/git", "/usr/bin/git", WithoutUploadPack()) - if err != nil { - t.Errorf("GitHTTPTransfer instance could not be created. %s", err.Error()) - return - } - - ts := httptest.NewServer(ght) - if ts == nil { - t.Error("test server is nil.") - } - defer ts.Close() - - res, err := http.Post( - ts.URL+"/test.git/git-upload-pack", - "application/x-git-upload-pack-request", - nil, - ) - if err != nil { - t.Errorf("http.Post: %s", err.Error()) - return - } - - if res.StatusCode != 403 { - t.Errorf("StatusCode is not 403. result: %d", res.StatusCode) - return + tests := []struct { + description string + url string + contentsType string + expectedCode int + gitHTTPTransferOption GitHTTPTransferOption + }{ + { + description: "it should return 403 if upload-pack is off", + url: "/test.git/git-upload-pack", + contentsType: "application/x-git-upload-pack-request", + expectedCode: 403, + gitHTTPTransferOption: WithoutUploadPack(), + }, + { + description: "it should return 403 if receive-pack is off", + url: "/test.git/git-receive-pack", + contentsType: "application/x-git-receive-pack-request", + expectedCode: 403, + gitHTTPTransferOption: WithoutReceivePack(), + }, + } + + for _, tc := range tests { + + t.Log(tc.description) + + ght, err := New("/data/git", "/usr/bin/git", tc.gitHTTPTransferOption) + if err != nil { + t.Errorf("GitHTTPTransfer instance could not be created. %s", err.Error()) + } + + ts := httptest.NewServer(ght) + if ts == nil { + t.Error("test server is nil.") + } + + res, err := http.Post( + ts.URL+tc.url, + tc.contentsType, + nil, + ) + if err != nil { + t.Errorf("http.Post: %s", err.Error()) + } + + if res.StatusCode != tc.expectedCode { + t.Errorf("StatusCode is not %d. result: %d", tc.expectedCode, res.StatusCode) + } + ts.Close() } } -func Test_it_should_return_403_if_receive_pack_is_off(t *testing.T) { - - if _, err := exec.LookPath("git"); err != nil { - log.Println("git is not found. so skip test.") - } - - ght, err := New("/data/git", "/usr/bin/git", WithoutReceivePack()) - if err != nil { - t.Errorf("GitHTTPTransfer instance could not be created. %s", err.Error()) - return - } - - ts := httptest.NewServer(ght) - if ts == nil { - t.Error("test server is nil.") - } - defer ts.Close() - - res, err := http.Post( - ts.URL+"/test.git/git-receive-pack", - "application/x-git-receive-pack-request", - nil, - ) - if err != nil { - t.Errorf("http.Post: %s", err.Error()) - return - } - - if res.StatusCode != 403 { - t.Errorf("StatusCode is not 403. result: %d", res.StatusCode) - return - } - -} -// TODO Could be converted to a table driven test with the next test -func Test_GitSmartHttp_MatchRouting_should_match_git_upload_pack(t *testing.T) { - ght, err := New("", "/usr/bin/git") - if err != nil { - t.Errorf("GitHTTPTransfer instance could not be created. %s", err.Error()) - return - } - m := http.MethodPost - p := "/base/foo/git-upload-pack" - expectedRepoPath := "/base/foo" - expectedFilePath := "git-upload-pack" - repoPath, filePath, _, err := ght.matchRouting(m, p) - if err != nil { - t.Errorf("error is %s", err.Error()) - return - } - if repoPath != expectedRepoPath { - t.Errorf("repository path is not %s . result: %s", expectedRepoPath, repoPath) - return - } - if filePath != expectedFilePath { - t.Errorf("file path is not %s . result: %s", expectedFilePath, filePath) - return - } -} - -func Test_GitSmartHttp_MatchRouting_should_not_match_if_http_method_is_different(t *testing.T) { +func Test_GitHTTPTransfer_MatchRouting_should_not_match(t *testing.T) { + t.Log("it should not match if http method is different") var err error ght, err := New("", "/usr/bin/git") if err != nil { @@ -126,204 +90,100 @@ func Test_GitSmartHttp_MatchRouting_should_not_match_if_http_method_is_different } } -// TODO Could be converted to a table driven test -// https://github.com/golang/go/wiki/TableDrivenTests -func Test_GitSmartHttp_MatchRouting_should_match_get_info_refs(t *testing.T) { +func Test_GitHTTPTransfer_MatchRouting_should_match(t *testing.T) { ght, err := New("", "/usr/bin/git") if err != nil { t.Errorf("GitHTTPTransfer instance could not be created. %s", err.Error()) return } - m := http.MethodGet - p := "/base/foo/info/refs" - expectedRepoPath := "/base/foo" - expectedFilePath := "info/refs" - repoPath, filePath, _, err := ght.matchRouting(m, p) - if err != nil { - t.Errorf("error is %s", err.Error()) - return - } - if repoPath != expectedRepoPath { - t.Errorf("repository path is not %s . result: %s", expectedRepoPath, repoPath) - return - } - if filePath != expectedFilePath { - t.Errorf("file path is not %s . result: %s", expectedFilePath, filePath) - return - } -} -func Test_GitSmartHttp_MatchRouting_should_match_get_head(t *testing.T) { - ght, err := New("", "/usr/bin/git") - if err != nil { - t.Errorf("GitHTTPTransfer instance could not be created. %s", err.Error()) - return - } - m := http.MethodGet - p := "/base/foo/HEAD" - expectedRepoPath := "/base/foo" - expectedFilePath := "HEAD" - repoPath, filePath, _, err := ght.matchRouting(m, p) - if err != nil { - t.Errorf("error is %s", err.Error()) - return - } - if repoPath != expectedRepoPath { - t.Errorf("repository path is not %s . result: %s", expectedRepoPath, repoPath) - return - } - if filePath != expectedFilePath { - t.Errorf("file path is not %s . result: %s", expectedFilePath, filePath) - return + tests := []struct { + description string + method string + path string + expectedRepoPath string + expectedFilePath string + }{ + { + description: "it should match git-upload-pack", + method: http.MethodPost, + path: "/base/foo/git-upload-pack", + expectedRepoPath: "/base/foo", + expectedFilePath: "git-upload-pack", + }, + { + description: "it should match get-info-refs", + method: http.MethodGet, + path: "/base/foo/info/refs", + expectedRepoPath: "/base/foo", + expectedFilePath : "info/refs", + }, + { + description: "it should match get-head", + method: http.MethodGet, + path: "/base/foo/HEAD", + expectedRepoPath: "/base/foo", + expectedFilePath : "HEAD", + }, + { + description: "it should match get-alternates", + method: http.MethodGet, + path: "/base/foo/objects/info/alternates", + expectedRepoPath: "/base/foo", + expectedFilePath : "objects/info/alternates", + }, + { + description: "it should match get-http-alternates", + method: http.MethodGet, + path: "/base/foo/objects/info/http-alternates", + expectedRepoPath: "/base/foo", + expectedFilePath : "objects/info/http-alternates", + }, + { + description: "it should match get-info-packs", + method: http.MethodGet, + path: "/base/foo/objects/info/packs", + expectedRepoPath: "/base/foo", + expectedFilePath : "objects/info/packs", + }, + { + description: "it should match get-loose-object", + method: http.MethodGet, + path: "/base/foo/objects/3b/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaacccccc", + expectedRepoPath: "/base/foo", + expectedFilePath : "objects/3b/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaacccccc", + }, + { + description: "it should match get-pack-file", + method: http.MethodGet, + path: "/base/foo/objects/pack/pack-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbb.pack", + expectedRepoPath: "/base/foo", + expectedFilePath : "objects/pack/pack-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbb.pack", + }, + { + description: "it should match get-idx-file", + method: http.MethodGet, + path: "/base/foo/objects/pack/pack-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbb.idx", + expectedRepoPath: "/base/foo", + expectedFilePath : "objects/pack/pack-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbb.idx", + }, + } + + for _, tc := range tests { + t.Log(tc.description) + repoPath, filePath, _, err := ght.matchRouting(tc.method, tc.path) + if err != nil { + t.Errorf("error is %s", err.Error()) + return + } + if repoPath != tc.expectedRepoPath { + t.Errorf("repository path is not %s . result: %s", tc.expectedRepoPath, repoPath) + return + } + if filePath != tc.expectedFilePath { + t.Errorf("file path is not %s . result: %s", tc.expectedFilePath, filePath) + return + } } } -func Test_GitSmartHttp_MatchRouting_should_match_get_alternates(t *testing.T) { - ght, err := New("", "/usr/bin/git") - if err != nil { - t.Errorf("GitHTTPTransfer instance could not be created. %s", err.Error()) - return - } - m := http.MethodGet - p := "/base/foo/objects/info/alternates" - expectedRepoPath := "/base/foo" - expectedFilePath := "objects/info/alternates" - repoPath, filePath, _, err := ght.matchRouting(m, p) - if err != nil { - t.Errorf("error is %s", err.Error()) - return - } - if repoPath != expectedRepoPath { - t.Errorf("repository path is not %s . result: %s", expectedRepoPath, repoPath) - return - } - if filePath != expectedFilePath { - t.Errorf("file path is not %s . result: %s", expectedFilePath, filePath) - return - } -} - -func Test_GitSmartHttp_MatchRouting_should_match_get_http_alternates(t *testing.T) { - ght, err := New("", "/usr/bin/git") - if err != nil { - t.Errorf("GitHTTPTransfer instance could not be created. %s", err.Error()) - return - } - m := http.MethodGet - p := "/base/foo/objects/info/http-alternates" - expectedRepoPath := "/base/foo" - expectedFilePath := "objects/info/http-alternates" - repoPath, filePath, _, err := ght.matchRouting(m, p) - if err != nil { - t.Errorf("error is %s", err.Error()) - return - } - if repoPath != expectedRepoPath { - t.Errorf("repository path is not %s . result: %s", expectedRepoPath, repoPath) - return - } - if filePath != expectedFilePath { - t.Errorf("file path is not %s . result: %s", expectedFilePath, filePath) - return - } -} - -func Test_GitSmartHttp_MatchRouting_should_match_get_info_packs(t *testing.T) { - ght, err := New("", "/usr/bin/git") - if err != nil { - t.Errorf("GitHTTPTransfer instance could not be created. %s", err.Error()) - return - } - m := http.MethodGet - p := "/base/foo/objects/info/packs" - expectedRepoPath := "/base/foo" - expectedFilePath := "objects/info/packs" - repoPath, filePath, _, err := ght.matchRouting(m, p) - if err != nil { - t.Errorf("error is %s", err.Error()) - return - } - if repoPath != expectedRepoPath { - t.Errorf("repository path is not %s . result: %s", expectedRepoPath, repoPath) - return - } - if filePath != expectedFilePath { - t.Errorf("file path is not %s . result: %s", expectedFilePath, filePath) - return - } -} - -func Test_GitSmartHttp_MatchRouting_should_match_get_loose_object(t *testing.T) { - ght, err := New("", "/usr/bin/git") - if err != nil { - t.Errorf("GitHTTPTransfer instance could not be created. %s", err.Error()) - return - } - m := http.MethodGet - p := "/base/foo/objects/3b/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaacccccc" - expectedRepoPath := "/base/foo" - expectedFilePath := "objects/3b/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaacccccc" - repoPath, filePath, _, err := ght.matchRouting(m, p) - if err != nil { - t.Errorf("error is %s", err.Error()) - return - } - if repoPath != expectedRepoPath { - t.Errorf("repository path is not %s . result: %s", expectedRepoPath, repoPath) - return - } - if filePath != expectedFilePath { - t.Errorf("file path is not %s . result: %s", expectedFilePath, filePath) - return - } -} - -func Test_GitSmartHttp_MatchRouting_should_match_get_pack_file(t *testing.T) { - ght, err := New("", "/usr/bin/git") - if err != nil { - t.Errorf("GitHTTPTransfer instance could not be created. %s", err.Error()) - return - } - m := http.MethodGet - p := "/base/foo/objects/pack/pack-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbb.pack" - expectedRepoPath := "/base/foo" - expectedFilePath := "objects/pack/pack-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbb.pack" - repoPath, filePath, _, err := ght.matchRouting(m, p) - if err != nil { - t.Errorf("error is %s", err.Error()) - return - } - if repoPath != expectedRepoPath { - t.Errorf("repository path is not %s . result: %s", expectedRepoPath, repoPath) - return - } - if filePath != expectedFilePath { - t.Errorf("file path is not %s . result: %s", expectedFilePath, filePath) - return - } -} - -func Test_GitSmartHttp_MatchRouting_should_match_get_idx_file(t *testing.T) { - ght, err := New("", "/usr/bin/git") - if err != nil { - t.Errorf("GitHTTPTransfer instance could not be created. %s", err.Error()) - return - } - m := http.MethodGet - p := "/base/foo/objects/pack/pack-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbb.idx" - expectedRepoPath := "/base/foo" - expectedFilePath := "objects/pack/pack-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbb.idx" - repoPath, filePath, _, err := ght.matchRouting(m, p) - if err != nil { - t.Errorf("error is %s", err.Error()) - return - } - if repoPath != expectedRepoPath { - t.Errorf("repository path is not %s . result: %s", expectedRepoPath, repoPath) - return - } - if filePath != expectedFilePath { - t.Errorf("file path is not %s . result: %s", expectedFilePath, filePath) - return - } -} diff --git a/githttptransfer/support_test.go b/githttptransfer/support_test.go index b81ef36..7028e53 100644 --- a/githttptransfer/support_test.go +++ b/githttptransfer/support_test.go @@ -6,40 +6,39 @@ import ( "testing" ) -// TODO Can be converted to a table driven test -// https://github.com/golang/go/wiki/TableDrivenTests -func Test_GetServiceType_should_return_git_upload_pack(t *testing.T) { - m := http.MethodPost - p := "http://example.com/base/foo/git-upload-pack?service=git-upload-pack" - r := httptest.NewRequest(m, p, nil) - - expected := "upload-pack" - - if serviceType := getServiceType(r); expected != serviceType { - t.Errorf("service type is not %s . result: %s", expected, serviceType) +func Test_GetServiceType(t *testing.T) { + + tests := []struct { + description string + method string + url string + expected string + }{ + { + description: "it should return upload-pack", + method: http.MethodPost, + url: "http://example.com/base/foo/git-upload-pack?service=git-upload-pack", + expected: "upload-pack", + }, + { + description: "it should return receive-pack", + method: http.MethodPost, + url: "http://example.com/base/foo/git-upload-pack?service=git-receive-pack", + expected: "receive-pack", + }, + { + description: "it should return empty", + method: http.MethodPost, + url: "http://example.com/base/foo/git-upload-pack?service=foo-receive-pack", + expected: "", + }, } -} - -func Test_GetServiceType_should_return_git_receive_pack(t *testing.T) { - m := http.MethodPost - p := "http://example.com/base/foo/git-receive-pack?service=git-receive-pack" - r := httptest.NewRequest(m, p, nil) - - expected := "receive-pack" - - if serviceType := getServiceType(r); expected != serviceType { - t.Errorf("service type is not %s . result: %s", expected, serviceType) - } -} - -func Test_GetServiceType_should_return_empty(t *testing.T) { - m := http.MethodPost - p := "http://example.com/base/foo/git-upload-pack?service=foo-upload-pack" - r := httptest.NewRequest(m, p, nil) - expected := "" + for _, tc := range tests { + r := httptest.NewRequest(tc.method, tc.url, nil) - if serviceType := getServiceType(r); expected != serviceType { - t.Errorf("service type is not %s . result: %s", "empty", serviceType) + if serviceType := getServiceType(r); tc.expected != serviceType { + t.Errorf("service type is not %s . result: %s", tc.expected, serviceType) + } } }