Unverified Commit 76a84455 authored by Sander van Harmelen's avatar Sander van Harmelen Committed by GitHub

Allow users to pass the base URL without the API info (#404)

* Allow users to pass the base URL without the API info

* Makes SetBaseURL more robust by matching on apiVersionPath suffix and adds unit test (#405)

* Fix tests
parent 1bfdd49b
...@@ -12,7 +12,7 @@ func TestListBroadcastMessages(t *testing.T) { ...@@ -12,7 +12,7 @@ func TestListBroadcastMessages(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/broadcast_messages", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/broadcast_messages", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprintf(w, `[{ fmt.Fprintf(w, `[{
"message": "Some Message", "message": "Some Message",
...@@ -71,7 +71,7 @@ func TestGetBroadcastMessages(t *testing.T) { ...@@ -71,7 +71,7 @@ func TestGetBroadcastMessages(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/broadcast_messages/1/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/broadcast_messages/1/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprintf(w, `{ fmt.Fprintf(w, `{
"message": "Some Message", "message": "Some Message",
...@@ -113,7 +113,7 @@ func TestCreateBroadcastMessages(t *testing.T) { ...@@ -113,7 +113,7 @@ func TestCreateBroadcastMessages(t *testing.T) {
wantedStartsAt := time.Date(2017, time.June, 26, 6, 0, 0, 0, time.UTC) wantedStartsAt := time.Date(2017, time.June, 26, 6, 0, 0, 0, time.UTC)
wantedEndsAt := time.Date(2017, time.June, 27, 12, 59, 0, 0, time.UTC) wantedEndsAt := time.Date(2017, time.June, 27, 12, 59, 0, 0, time.UTC)
mux.HandleFunc("/broadcast_messages", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/broadcast_messages", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST") testMethod(t, r, "POST")
fmt.Fprintf(w, `{ fmt.Fprintf(w, `{
"message": "Some Message", "message": "Some Message",
...@@ -161,7 +161,7 @@ func TestUpdateBroadcastMessages(t *testing.T) { ...@@ -161,7 +161,7 @@ func TestUpdateBroadcastMessages(t *testing.T) {
wantedStartsAt := time.Date(2017, time.June, 26, 6, 0, 0, 0, time.UTC) wantedStartsAt := time.Date(2017, time.June, 26, 6, 0, 0, 0, time.UTC)
wantedEndsAt := time.Date(2017, time.June, 27, 12, 59, 0, 0, time.UTC) wantedEndsAt := time.Date(2017, time.June, 27, 12, 59, 0, 0, time.UTC)
mux.HandleFunc("/broadcast_messages/1", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/broadcast_messages/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT") testMethod(t, r, "PUT")
fmt.Fprintf(w, `{ fmt.Fprintf(w, `{
"message": "Some Message Updated", "message": "Some Message Updated",
...@@ -206,7 +206,7 @@ func TestDeleteBroadcastMessages(t *testing.T) { ...@@ -206,7 +206,7 @@ func TestDeleteBroadcastMessages(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/broadcast_messages/1", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/broadcast_messages/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE") testMethod(t, r, "DELETE")
}) })
......
...@@ -19,7 +19,7 @@ func TestListBuildVariables(t *testing.T) { ...@@ -19,7 +19,7 @@ func TestListBuildVariables(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/variables", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/variables", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprintf(w, fmt.Fprintf(w,
`[{"key":"%s","value":"%s"},{"key":"%s","value":"%s"}]`, myKey, myValue, myKey2, myValue2) `[{"key":"%s","value":"%s"},{"key":"%s","value":"%s"}]`, myKey, myValue, myKey2, myValue2)
...@@ -40,7 +40,7 @@ func TestGetBuildVariable(t *testing.T) { ...@@ -40,7 +40,7 @@ func TestGetBuildVariable(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/variables/"+myKey, func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/variables/"+myKey, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprintf(w, `{"key":"%s","value":"%s"}`, myKey, myValue) fmt.Fprintf(w, `{"key":"%s","value":"%s"}`, myKey, myValue)
}) })
...@@ -60,7 +60,7 @@ func TestCreateBuildVariable(t *testing.T) { ...@@ -60,7 +60,7 @@ func TestCreateBuildVariable(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/variables", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/variables", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST") testMethod(t, r, "POST")
fmt.Fprintf(w, `{"key":"%s","value":"%s", "protected": false}`, myKey, myValue) fmt.Fprintf(w, `{"key":"%s","value":"%s", "protected": false}`, myKey, myValue)
}) })
...@@ -81,7 +81,7 @@ func TestUpdateBuildVariable(t *testing.T) { ...@@ -81,7 +81,7 @@ func TestUpdateBuildVariable(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/variables/"+myKey, func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/variables/"+myKey, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT") testMethod(t, r, "PUT")
fmt.Fprintf(w, `{"key":"%s","value":"%s", "protected": false}`, myKey, myNewValue) fmt.Fprintf(w, `{"key":"%s","value":"%s", "protected": false}`, myKey, myNewValue)
}) })
...@@ -102,7 +102,7 @@ func TestRemoveBuildVariable(t *testing.T) { ...@@ -102,7 +102,7 @@ func TestRemoveBuildVariable(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/variables/"+myKey, func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/variables/"+myKey, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE") testMethod(t, r, "DELETE")
}) })
......
...@@ -11,7 +11,7 @@ func TestGetCommitStatuses(t *testing.T) { ...@@ -11,7 +11,7 @@ func TestGetCommitStatuses(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/repository/commits/b0b3a907f41409829b307a28b82fdbd552ee5a27/statuses", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/repository/commits/b0b3a907f41409829b307a28b82fdbd552ee5a27/statuses", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1}]`) fmt.Fprint(w, `[{"id":1}]`)
}) })
...@@ -33,7 +33,7 @@ func TestSetCommitStatus(t *testing.T) { ...@@ -33,7 +33,7 @@ func TestSetCommitStatus(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/statuses/b0b3a907f41409829b307a28b82fdbd552ee5a27", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/statuses/b0b3a907f41409829b307a28b82fdbd552ee5a27", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST") testMethod(t, r, "POST")
fmt.Fprint(w, `{"id":1}`) fmt.Fprint(w, `{"id":1}`)
}) })
......
...@@ -11,7 +11,7 @@ func TestListFeatureFlags(t *testing.T) { ...@@ -11,7 +11,7 @@ func TestListFeatureFlags(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/features", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/features", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, ` fmt.Fprint(w, `
[ [
...@@ -53,7 +53,7 @@ func TestSetFeatureFlag(t *testing.T) { ...@@ -53,7 +53,7 @@ func TestSetFeatureFlag(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/features/new_library", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/features/new_library", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST") testMethod(t, r, "POST")
fmt.Fprint(w, ` fmt.Fprint(w, `
{ {
......
...@@ -36,7 +36,8 @@ import ( ...@@ -36,7 +36,8 @@ import (
) )
const ( const (
defaultBaseURL = "https://gitlab.com/api/v4/" defaultBaseURL = "https://gitlab.com/"
apiVersionPath = "api/v4/"
userAgent = "go-gitlab" userAgent = "go-gitlab"
) )
...@@ -350,7 +351,7 @@ func NewBasicAuthClient(httpClient *http.Client, endpoint, username, password st ...@@ -350,7 +351,7 @@ func NewBasicAuthClient(httpClient *http.Client, endpoint, username, password st
client.authType = basicAuth client.authType = basicAuth
client.username = username client.username = username
client.password = password client.password = password
client.SetBaseURL(endpoint + "/api/v4") client.SetBaseURL(endpoint)
err := client.requestOAuthToken(context.TODO()) err := client.requestOAuthToken(context.TODO())
if err != nil { if err != nil {
...@@ -468,9 +469,19 @@ func (c *Client) SetBaseURL(urlStr string) error { ...@@ -468,9 +469,19 @@ func (c *Client) SetBaseURL(urlStr string) error {
urlStr += "/" urlStr += "/"
} }
var err error baseURL, err := url.Parse(urlStr)
c.baseURL, err = url.Parse(urlStr) if err != nil {
return err return err
}
if !strings.HasSuffix(baseURL.Path, apiVersionPath) {
baseURL.Path += apiVersionPath
}
// Update the base URL of the client.
c.baseURL = baseURL
return nil
} }
// NewRequest creates an API request. A relative URL path can be provided in // NewRequest creates an API request. A relative URL path can be provided in
......
...@@ -45,15 +45,28 @@ func testMethod(t *testing.T, r *http.Request, want string) { ...@@ -45,15 +45,28 @@ func testMethod(t *testing.T, r *http.Request, want string) {
func TestNewClient(t *testing.T) { func TestNewClient(t *testing.T) {
c := NewClient(nil, "") c := NewClient(nil, "")
expectedBaseURL := defaultBaseURL + apiVersionPath
if c.BaseURL().String() != defaultBaseURL { if c.BaseURL().String() != expectedBaseURL {
t.Errorf("NewClient BaseURL is %s, want %s", c.BaseURL().String(), defaultBaseURL) t.Errorf("NewClient BaseURL is %s, want %s", c.BaseURL().String(), expectedBaseURL)
} }
if c.UserAgent != userAgent { if c.UserAgent != userAgent {
t.Errorf("NewClient UserAgent is %s, want %s", c.UserAgent, userAgent) t.Errorf("NewClient UserAgent is %s, want %s", c.UserAgent, userAgent)
} }
} }
func TestSetBaseURL(t *testing.T) {
expectedBaseURL := "http://gitlab.local/foo/" + apiVersionPath
c := NewClient(nil, "")
err := c.SetBaseURL("http://gitlab.local/foo")
if err != nil {
t.Fatalf("Failed to SetBaseURL: %v", err)
}
if c.BaseURL().String() != expectedBaseURL {
t.Errorf("BaseURL is %s, want %s", c.BaseURL().String(), expectedBaseURL)
}
}
func TestCheckResponse(t *testing.T) { func TestCheckResponse(t *testing.T) {
req, err := NewClient(nil, "").NewRequest("GET", "test", nil, nil) req, err := NewClient(nil, "").NewRequest("GET", "test", nil, nil)
if err != nil { if err != nil {
......
...@@ -11,7 +11,7 @@ func TestListPipelineJobs(t *testing.T) { ...@@ -11,7 +11,7 @@ func TestListPipelineJobs(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/pipelines/1/jobs", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/pipelines/1/jobs", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`) fmt.Fprint(w, `[{"id":1},{"id":2}]`)
}) })
......
...@@ -11,7 +11,7 @@ func TestRunPipeline(t *testing.T) { ...@@ -11,7 +11,7 @@ func TestRunPipeline(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/trigger/pipeline", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/trigger/pipeline", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST") testMethod(t, r, "POST")
fmt.Fprint(w, `{"id":1, "status":"pending"}`) fmt.Fprint(w, `{"id":1, "status":"pending"}`)
}) })
......
...@@ -11,7 +11,7 @@ func TestListProjectPipelines(t *testing.T) { ...@@ -11,7 +11,7 @@ func TestListProjectPipelines(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/pipelines", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/pipelines", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`) fmt.Fprint(w, `[{"id":1},{"id":2}]`)
}) })
...@@ -32,7 +32,7 @@ func TestGetPipeline(t *testing.T) { ...@@ -32,7 +32,7 @@ func TestGetPipeline(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/pipelines/5949167", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/pipelines/5949167", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1,"status":"success"}`) fmt.Fprint(w, `{"id":1,"status":"success"}`)
}) })
...@@ -52,7 +52,7 @@ func TestCreatePipeline(t *testing.T) { ...@@ -52,7 +52,7 @@ func TestCreatePipeline(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/pipeline", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/pipeline", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST") testMethod(t, r, "POST")
fmt.Fprint(w, `{"id":1, "status":"pending"}`) fmt.Fprint(w, `{"id":1, "status":"pending"}`)
}) })
...@@ -74,7 +74,7 @@ func TestRetryPipelineBuild(t *testing.T) { ...@@ -74,7 +74,7 @@ func TestRetryPipelineBuild(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/pipelines/5949167/retry", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/pipelines/5949167/retry", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST") testMethod(t, r, "POST")
fmt.Fprintln(w, `{"id":1, "status":"pending"}`) fmt.Fprintln(w, `{"id":1, "status":"pending"}`)
}) })
...@@ -94,7 +94,7 @@ func TestCancelPipelineBuild(t *testing.T) { ...@@ -94,7 +94,7 @@ func TestCancelPipelineBuild(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/pipelines/5949167/cancel", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/pipelines/5949167/cancel", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST") testMethod(t, r, "POST")
fmt.Fprintln(w, `{"id":1, "status":"canceled"}`) fmt.Fprintln(w, `{"id":1, "status":"canceled"}`)
}) })
......
...@@ -14,7 +14,7 @@ func TestListProjects(t *testing.T) { ...@@ -14,7 +14,7 @@ func TestListProjects(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`) fmt.Fprint(w, `[{"id":1},{"id":2}]`)
}) })
...@@ -44,7 +44,7 @@ func TestListUserProjects(t *testing.T) { ...@@ -44,7 +44,7 @@ func TestListUserProjects(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/users/1/projects", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/users/1/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`) fmt.Fprint(w, `[{"id":1},{"id":2}]`)
}) })
...@@ -74,8 +74,8 @@ func TestListProjectsUsersByID(t *testing.T) { ...@@ -74,8 +74,8 @@ func TestListProjectsUsersByID(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/", func(w http.ResponseWriter, r *http.Request) {
testURL(t, r, "/projects/1/users?page=2&per_page=3&search=query") testURL(t, r, "/api/v4/projects/1/users?page=2&per_page=3&search=query")
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`) fmt.Fprint(w, `[{"id":1},{"id":2}]`)
}) })
...@@ -100,8 +100,8 @@ func TestListProjectsUsersByName(t *testing.T) { ...@@ -100,8 +100,8 @@ func TestListProjectsUsersByName(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/", func(w http.ResponseWriter, r *http.Request) {
testURL(t, r, "/projects/namespace%2Fname/users?page=2&per_page=3&search=query") testURL(t, r, "/api/v4/projects/namespace%2Fname/users?page=2&per_page=3&search=query")
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`) fmt.Fprint(w, `[{"id":1},{"id":2}]`)
}) })
...@@ -126,7 +126,7 @@ func TestListOwnedProjects(t *testing.T) { ...@@ -126,7 +126,7 @@ func TestListOwnedProjects(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`) fmt.Fprint(w, `[{"id":1},{"id":2}]`)
}) })
...@@ -157,7 +157,7 @@ func TestListStarredProjects(t *testing.T) { ...@@ -157,7 +157,7 @@ func TestListStarredProjects(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`) fmt.Fprint(w, `[{"id":1},{"id":2}]`)
}) })
...@@ -188,7 +188,7 @@ func TestGetProjectByID(t *testing.T) { ...@@ -188,7 +188,7 @@ func TestGetProjectByID(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1}`) fmt.Fprint(w, `{"id":1}`)
}) })
...@@ -208,8 +208,8 @@ func TestGetProjectByName(t *testing.T) { ...@@ -208,8 +208,8 @@ func TestGetProjectByName(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/", func(w http.ResponseWriter, r *http.Request) {
testURL(t, r, "/projects/namespace%2Fname") testURL(t, r, "/api/v4/projects/namespace%2Fname")
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1}`) fmt.Fprint(w, `{"id":1}`)
}) })
...@@ -229,7 +229,7 @@ func TestCreateProject(t *testing.T) { ...@@ -229,7 +229,7 @@ func TestCreateProject(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST") testMethod(t, r, "POST")
fmt.Fprint(w, `{"id":1}`) fmt.Fprint(w, `{"id":1}`)
}) })
...@@ -254,7 +254,7 @@ func TestUploadFile(t *testing.T) { ...@@ -254,7 +254,7 @@ func TestUploadFile(t *testing.T) {
tf, _ := ioutil.TempFile(os.TempDir(), "test") tf, _ := ioutil.TempFile(os.TempDir(), "test")
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
mux.HandleFunc("/projects/1/uploads", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/uploads", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost) testMethod(t, r, http.MethodPost)
if false == strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data;") { if false == strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data;") {
t.Fatalf("Prokects.UploadFile request content-type %+v want multipart/form-data;", r.Header.Get("Content-Type")) t.Fatalf("Prokects.UploadFile request content-type %+v want multipart/form-data;", r.Header.Get("Content-Type"))
...@@ -290,11 +290,8 @@ func TestListProjectForks(t *testing.T) { ...@@ -290,11 +290,8 @@ func TestListProjectForks(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/", func(w http.ResponseWriter, r *http.Request) {
want := "/projects/namespace%2Fname/forks" testURL(t, r, "/api/v4/projects/namespace%2Fname/forks?archived=true&order_by=name&page=2&per_page=3&search=query&simple=true&sort=asc&visibility=public")
if !strings.HasPrefix(r.RequestURI, want) {
t.Errorf("Request url: %+v, should have prefix %s", r.RequestURI, want)
}
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`) fmt.Fprint(w, `[{"id":1},{"id":2}]`)
}) })
...@@ -323,7 +320,7 @@ func TestShareProjectWithGroup(t *testing.T) { ...@@ -323,7 +320,7 @@ func TestShareProjectWithGroup(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/share", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/share", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST") testMethod(t, r, "POST")
}) })
...@@ -342,7 +339,7 @@ func TestDeleteSharedProjectFromGroup(t *testing.T) { ...@@ -342,7 +339,7 @@ func TestDeleteSharedProjectFromGroup(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/share/2", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/share/2", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE") testMethod(t, r, "DELETE")
}) })
......
...@@ -28,7 +28,7 @@ func TestDisableRunner(t *testing.T) { ...@@ -28,7 +28,7 @@ func TestDisableRunner(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/runners/2", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/runners/2", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE") testMethod(t, r, "DELETE")
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
}) })
...@@ -43,7 +43,7 @@ func TestListRunnersJobs(t *testing.T) { ...@@ -43,7 +43,7 @@ func TestListRunnersJobs(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/runners/1/jobs", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/runners/1/jobs", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`) fmt.Fprint(w, `[{"id":1},{"id":2}]`)
}) })
...@@ -65,7 +65,7 @@ func TestRemoveRunner(t *testing.T) { ...@@ -65,7 +65,7 @@ func TestRemoveRunner(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/runners/1", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/runners/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE") testMethod(t, r, "DELETE")
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
}) })
...@@ -110,7 +110,7 @@ func TestUpdateRunnersDetails(t *testing.T) { ...@@ -110,7 +110,7 @@ func TestUpdateRunnersDetails(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/runners/6", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/runners/6", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT") testMethod(t, r, "PUT")
fmt.Fprint(w, exampleDetailRsp) fmt.Fprint(w, exampleDetailRsp)
}) })
...@@ -132,7 +132,7 @@ func TestGetRunnerDetails(t *testing.T) { ...@@ -132,7 +132,7 @@ func TestGetRunnerDetails(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/runners/6", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/runners/6", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, exampleDetailRsp) fmt.Fprint(w, exampleDetailRsp)
}) })
......
...@@ -11,7 +11,7 @@ func TestGetDroneCIService(t *testing.T) { ...@@ -11,7 +11,7 @@ func TestGetDroneCIService(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/services/drone-ci", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/services/drone-ci", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1}`) fmt.Fprint(w, `{"id":1}`)
}) })
...@@ -30,7 +30,7 @@ func TestSetDroneCIService(t *testing.T) { ...@@ -30,7 +30,7 @@ func TestSetDroneCIService(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/services/drone-ci", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/services/drone-ci", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT") testMethod(t, r, "PUT")
}) })
...@@ -46,7 +46,7 @@ func TestDeleteDroneCIService(t *testing.T) { ...@@ -46,7 +46,7 @@ func TestDeleteDroneCIService(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/services/drone-ci", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/services/drone-ci", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE") testMethod(t, r, "DELETE")
}) })
...@@ -60,7 +60,7 @@ func TestGetSlackService(t *testing.T) { ...@@ -60,7 +60,7 @@ func TestGetSlackService(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/services/slack", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/services/slack", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1}`) fmt.Fprint(w, `{"id":1}`)
}) })
...@@ -79,7 +79,7 @@ func TestSetSlackService(t *testing.T) { ...@@ -79,7 +79,7 @@ func TestSetSlackService(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/services/slack", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/services/slack", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT") testMethod(t, r, "PUT")
}) })
...@@ -99,7 +99,7 @@ func TestDeleteSlackService(t *testing.T) { ...@@ -99,7 +99,7 @@ func TestDeleteSlackService(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/services/slack", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/services/slack", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE") testMethod(t, r, "DELETE")
}) })
...@@ -113,7 +113,7 @@ func TestGetJiraService(t *testing.T) { ...@@ -113,7 +113,7 @@ func TestGetJiraService(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/services/jira", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/services/jira", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1}`) fmt.Fprint(w, `{"id":1}`)
}) })
...@@ -132,7 +132,7 @@ func TestSetJiraService(t *testing.T) { ...@@ -132,7 +132,7 @@ func TestSetJiraService(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/services/jira", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/services/jira", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT") testMethod(t, r, "PUT")
}) })
...@@ -154,7 +154,7 @@ func TestDeleteJiraService(t *testing.T) { ...@@ -154,7 +154,7 @@ func TestDeleteJiraService(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/services/jira", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/services/jira", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE") testMethod(t, r, "DELETE")
}) })
......
...@@ -11,7 +11,7 @@ func TestListTags(t *testing.T) { ...@@ -11,7 +11,7 @@ func TestListTags(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/repository/tags", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/repository/tags", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `[{"name": "1.0.0"},{"name": "1.0.1"}]`) fmt.Fprint(w, `[{"name": "1.0.0"},{"name": "1.0.1"}]`)
}) })
...@@ -33,7 +33,7 @@ func TestCreateRelease(t *testing.T) { ...@@ -33,7 +33,7 @@ func TestCreateRelease(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/repository/tags/1.0.0/release", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/repository/tags/1.0.0/release", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST") testMethod(t, r, "POST")
fmt.Fprint(w, `{"tag_name": "1.0.0", "description": "Amazing release. Wow"}`) fmt.Fprint(w, `{"tag_name": "1.0.0", "description": "Amazing release. Wow"}`)
}) })
...@@ -55,7 +55,7 @@ func TestUpdateRelease(t *testing.T) { ...@@ -55,7 +55,7 @@ func TestUpdateRelease(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/projects/1/repository/tags/1.0.0/release", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/projects/1/repository/tags/1.0.0/release", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT") testMethod(t, r, "PUT")
fmt.Fprint(w, `{"tag_name": "1.0.0", "description": "Amazing release. Wow!"}`) fmt.Fprint(w, `{"tag_name": "1.0.0", "description": "Amazing release. Wow!"}`)
}) })
......
...@@ -11,7 +11,7 @@ func TestListTodos(t *testing.T) { ...@@ -11,7 +11,7 @@ func TestListTodos(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/todos", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/todos", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1,"state": "pending"},{"id":2,"state":"pending"}]`) fmt.Fprint(w, `[{"id":1,"state": "pending"},{"id":2,"state":"pending"}]`)
}) })
...@@ -34,7 +34,7 @@ func TestMarkAllTodosAsDone(t *testing.T) { ...@@ -34,7 +34,7 @@ func TestMarkAllTodosAsDone(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/todos/mark_as_done", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/todos/mark_as_done", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST") testMethod(t, r, "POST")
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
}) })
...@@ -50,7 +50,7 @@ func TestMarkTodoAsDone(t *testing.T) { ...@@ -50,7 +50,7 @@ func TestMarkTodoAsDone(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/todos/1/mark_as_done", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/todos/1/mark_as_done", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST") testMethod(t, r, "POST")
}) })
......
...@@ -51,7 +51,7 @@ func TestValidate(t *testing.T) { ...@@ -51,7 +51,7 @@ func TestValidate(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/ci/lint", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/v4/ci/lint", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST") testMethod(t, r, "POST")
fmt.Fprint(w, tc.response) fmt.Fprint(w, tc.response)
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment