Skip to content

Commit

Permalink
Allow users to pass the base URL without the API info (xanzy#404)
Browse files Browse the repository at this point in the history
* Allow users to pass the base URL without the API info

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

* Fix tests
  • Loading branch information
Sander van Harmelen authored May 22, 2018
1 parent 1bfdd49 commit 76a8445
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 69 deletions.
10 changes: 5 additions & 5 deletions broadcast_messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestListBroadcastMessages(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprintf(w, `[{
"message": "Some Message",
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestGetBroadcastMessages(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprintf(w, `{
"message": "Some Message",
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestCreateBroadcastMessages(t *testing.T) {
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)

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")
fmt.Fprintf(w, `{
"message": "Some Message",
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestUpdateBroadcastMessages(t *testing.T) {
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)

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")
fmt.Fprintf(w, `{
"message": "Some Message Updated",
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestDeleteBroadcastMessages(t *testing.T) {
mux, server, client := setup()
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")
})

Expand Down
10 changes: 5 additions & 5 deletions build_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestListBuildVariables(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprintf(w,
`[{"key":"%s","value":"%s"},{"key":"%s","value":"%s"}]`, myKey, myValue, myKey2, myValue2)
Expand All @@ -40,7 +40,7 @@ func TestGetBuildVariable(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprintf(w, `{"key":"%s","value":"%s"}`, myKey, myValue)
})
Expand All @@ -60,7 +60,7 @@ func TestCreateBuildVariable(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprintf(w, `{"key":"%s","value":"%s", "protected": false}`, myKey, myValue)
})
Expand All @@ -81,7 +81,7 @@ func TestUpdateBuildVariable(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprintf(w, `{"key":"%s","value":"%s", "protected": false}`, myKey, myNewValue)
})
Expand All @@ -102,7 +102,7 @@ func TestRemoveBuildVariable(t *testing.T) {
mux, server, client := setup()
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")
})

Expand Down
4 changes: 2 additions & 2 deletions commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestGetCommitStatuses(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `[{"id":1}]`)
})
Expand All @@ -33,7 +33,7 @@ func TestSetCommitStatus(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `{"id":1}`)
})
Expand Down
4 changes: 2 additions & 2 deletions feature_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestListFeatureFlags(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `
[
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestSetFeatureFlag(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `
{
Expand Down
21 changes: 16 additions & 5 deletions gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import (
)

const (
defaultBaseURL = "https://gitlab.com/api/v4/"
defaultBaseURL = "https://gitlab.com/"
apiVersionPath = "api/v4/"
userAgent = "go-gitlab"
)

Expand Down Expand Up @@ -350,7 +351,7 @@ func NewBasicAuthClient(httpClient *http.Client, endpoint, username, password st
client.authType = basicAuth
client.username = username
client.password = password
client.SetBaseURL(endpoint + "/api/v4")
client.SetBaseURL(endpoint)

err := client.requestOAuthToken(context.TODO())
if err != nil {
Expand Down Expand Up @@ -468,9 +469,19 @@ func (c *Client) SetBaseURL(urlStr string) error {
urlStr += "/"
}

var err error
c.baseURL, err = url.Parse(urlStr)
return err
baseURL, err := url.Parse(urlStr)
if err != nil {
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
Expand Down
17 changes: 15 additions & 2 deletions gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,28 @@ func testMethod(t *testing.T, r *http.Request, want string) {

func TestNewClient(t *testing.T) {
c := NewClient(nil, "")
expectedBaseURL := defaultBaseURL + apiVersionPath

if c.BaseURL().String() != defaultBaseURL {
t.Errorf("NewClient BaseURL is %s, want %s", c.BaseURL().String(), defaultBaseURL)
if c.BaseURL().String() != expectedBaseURL {
t.Errorf("NewClient BaseURL is %s, want %s", c.BaseURL().String(), expectedBaseURL)
}
if 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) {
req, err := NewClient(nil, "").NewRequest("GET", "test", nil, nil)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestListPipelineJobs(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
Expand Down
2 changes: 1 addition & 1 deletion pipeline_triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestRunPipeline(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `{"id":1, "status":"pending"}`)
})
Expand Down
10 changes: 5 additions & 5 deletions pipelines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestListProjectPipelines(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
Expand All @@ -32,7 +32,7 @@ func TestGetPipeline(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `{"id":1,"status":"success"}`)
})
Expand All @@ -52,7 +52,7 @@ func TestCreatePipeline(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `{"id":1, "status":"pending"}`)
})
Expand All @@ -74,7 +74,7 @@ func TestRetryPipelineBuild(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprintln(w, `{"id":1, "status":"pending"}`)
})
Expand All @@ -94,7 +94,7 @@ func TestCancelPipelineBuild(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprintln(w, `{"id":1, "status":"canceled"}`)
})
Expand Down
37 changes: 17 additions & 20 deletions projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestListProjects(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestListUserProjects(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
Expand Down Expand Up @@ -74,8 +74,8 @@ func TestListProjectsUsersByID(t *testing.T) {
mux, server, client := setup()
defer teardown(server)

mux.HandleFunc("/projects/", func(w http.ResponseWriter, r *http.Request) {
testURL(t, r, "/projects/1/users?page=2&per_page=3&search=query")
mux.HandleFunc("/api/v4/projects/", func(w http.ResponseWriter, r *http.Request) {
testURL(t, r, "/api/v4/projects/1/users?page=2&per_page=3&search=query")
testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
Expand All @@ -100,8 +100,8 @@ func TestListProjectsUsersByName(t *testing.T) {
mux, server, client := setup()
defer teardown(server)

mux.HandleFunc("/projects/", func(w http.ResponseWriter, r *http.Request) {
testURL(t, r, "/projects/namespace%2Fname/users?page=2&per_page=3&search=query")
mux.HandleFunc("/api/v4/projects/", func(w http.ResponseWriter, r *http.Request) {
testURL(t, r, "/api/v4/projects/namespace%2Fname/users?page=2&per_page=3&search=query")
testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
Expand All @@ -126,7 +126,7 @@ func TestListOwnedProjects(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestListStarredProjects(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestGetProjectByID(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `{"id":1}`)
})
Expand All @@ -208,8 +208,8 @@ func TestGetProjectByName(t *testing.T) {
mux, server, client := setup()
defer teardown(server)

mux.HandleFunc("/projects/", func(w http.ResponseWriter, r *http.Request) {
testURL(t, r, "/projects/namespace%2Fname")
mux.HandleFunc("/api/v4/projects/", func(w http.ResponseWriter, r *http.Request) {
testURL(t, r, "/api/v4/projects/namespace%2Fname")
testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1}`)
})
Expand All @@ -229,7 +229,7 @@ func TestCreateProject(t *testing.T) {
mux, server, client := setup()
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")
fmt.Fprint(w, `{"id":1}`)
})
Expand All @@ -254,7 +254,7 @@ func TestUploadFile(t *testing.T) {
tf, _ := ioutil.TempFile(os.TempDir(), "test")
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)
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"))
Expand Down Expand Up @@ -290,11 +290,8 @@ func TestListProjectForks(t *testing.T) {
mux, server, client := setup()
defer teardown(server)

mux.HandleFunc("/projects/", func(w http.ResponseWriter, r *http.Request) {
want := "/projects/namespace%2Fname/forks"
if !strings.HasPrefix(r.RequestURI, want) {
t.Errorf("Request url: %+v, should have prefix %s", r.RequestURI, want)
}
mux.HandleFunc("/api/v4/projects/", func(w http.ResponseWriter, r *http.Request) {
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")
testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
Expand Down Expand Up @@ -323,7 +320,7 @@ func TestShareProjectWithGroup(t *testing.T) {
mux, server, client := setup()
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")
})

Expand All @@ -342,7 +339,7 @@ func TestDeleteSharedProjectFromGroup(t *testing.T) {
mux, server, client := setup()
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")
})

Expand Down
Loading

0 comments on commit 76a8445

Please sign in to comment.