Verified Commit be0528f0 authored by Patrick Rice's avatar Patrick Rice Committed by GitLab
Browse files

fix: Fixed error where GraphQL calls on subpaths didn't work properly

Changelog: Improvements
parent b3558b1a
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -8,7 +8,9 @@

	// Features add additional features to your environment. See https://containers.dev/features
	"features": {
        "ghcr.io/devcontainers/features/docker-in-docker:2": {},
        "ghcr.io/devcontainers/features/docker-in-docker:2": {
			"moby": false
		},
		"ghcr.io/devcontainers-extra/features/mise:1": {},
		"ghcr.io/devcontainers/features/go:1": {
			"version": "1.25"
+9 −1
Original line number Diff line number Diff line
@@ -57,6 +57,14 @@ var (
// configured to talk to that test server.  Tests should register handlers on
// mux which provide mock responses for the API method being tested.
func setup(t *testing.T) (*http.ServeMux, *Client) {
	// Setup on subpath, but pass in an empty subpath.
	return setupOnSubpath(t, "")
}

// setupOnSubpath sets up a test HTTP server along with a gitlab.Client that is
// configured to talk to that test server, but it sets up the client to use a baseURL
// configured on a subpath instead of the root of the domain.
func setupOnSubpath(t *testing.T, path string) (*http.ServeMux, *Client) {
	// mux is the HTTP request multiplexer used with the test server.
	mux := http.NewServeMux()

@@ -66,7 +74,7 @@ func setup(t *testing.T) (*http.ServeMux, *Client) {

	// client is the GitLab client being tested.
	client, err := NewClient("",
		WithBaseURL(server.URL),
		WithBaseURL(server.URL+path),
		// Disable backoff to speed up tests that expect errors.
		WithCustomBackoff(func(_, _ time.Duration, _ int, _ *http.Response) time.Duration {
			return 0
+2 −1
Original line number Diff line number Diff line
@@ -84,8 +84,9 @@ func (g *GraphQL) Do(query GraphQLQuery, response any, options ...RequestOptionF
	if err != nil {
		return nil, fmt.Errorf("failed to create GraphQL request: %w", err)
	}

	// Overwrite the path of the existing request, as otherwise client-go appends /api/v4 instead.
	request.URL.Path = GraphQLAPIEndpoint
	request.URL.Path = strings.Replace(request.URL.Path, "/"+apiVersionPath, GraphQLAPIEndpoint, 1)
	resp, err := g.client.Do(request, response)
	if err != nil {
		// return error, details can be read from Response
+34 −0
Original line number Diff line number Diff line
@@ -43,6 +43,40 @@ func TestGraphQL_Do_Success(t *testing.T) {
	assert.Equal(t, "any-id", response.Data.Project.ID)
}

func TestGraphQL_Do_Success_With_Subpath(t *testing.T) {
	t.Parallel()

	// GIVEN
	mux, client := setupOnSubpath(t, "/subpath")
	mux.HandleFunc("/subpath/api/graphql", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodPost)
		testJSONBody(t, r, `{ "query": "query { project(fullPath: \"gitlab-org/gitlab\") { id } }" }`)
		fmt.Fprint(w, `
			{
				"data": {
					"project": {
						"id": "any-id"
					}
				}
			}
		`)
	})

	// WHEN
	var response struct {
		Data struct {
			Project struct {
				ID string `json:"id"`
			} `json:"project"`
		} `json:"data"`
	}
	_, err := client.GraphQL.Do(GraphQLQuery{Query: `query { project(fullPath: "gitlab-org/gitlab") { id } }`}, &response)

	// THEN
	require.NoError(t, err)
	assert.Equal(t, "any-id", response.Data.Project.ID)
}

func TestGraphQL_Do_Success_With_Variables(t *testing.T) {
	t.Parallel()