2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2019-09-14 12:21:16 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2019-09-14 12:21:16 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2019-09-14 12:21:16 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-08-22 19:06:56 +00:00
|
|
|
package testdata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-10-03 12:42:47 +00:00
|
|
|
"testing"
|
2015-08-22 19:06:56 +00:00
|
|
|
)
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
// NewServer setup a mock server for testing purposes.
|
|
|
|
func NewServer(t *testing.T) *httptest.Server {
|
2015-08-22 19:06:56 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
|
|
|
|
// handle requests and serve mock data
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2022-11-04 23:35:06 +00:00
|
|
|
t.Logf("gitlab forge mock server: [%s] %s", r.Method, r.URL.Path)
|
2015-08-22 19:06:56 +00:00
|
|
|
// evaluate the path to serve a dummy data file
|
2021-10-03 12:42:47 +00:00
|
|
|
|
|
|
|
// TODO: find source of "/api/v4/" requests
|
|
|
|
// assert.EqualValues(t, "go-gitlab", r.Header.Get("user-agent"), "on request: "+r.URL.Path)
|
|
|
|
|
2015-08-22 19:06:56 +00:00
|
|
|
switch r.URL.Path {
|
2017-08-10 06:24:35 +00:00
|
|
|
case "/api/v4/projects":
|
2021-10-03 12:42:47 +00:00
|
|
|
if r.FormValue("archived") == "false" {
|
2016-02-16 11:35:20 +00:00
|
|
|
w.Write(notArchivedProjectsPayload)
|
|
|
|
} else {
|
|
|
|
w.Write(allProjectsPayload)
|
|
|
|
}
|
|
|
|
|
2015-08-22 19:06:56 +00:00
|
|
|
return
|
2017-08-10 06:24:35 +00:00
|
|
|
case "/api/v4/projects/diaspora/diaspora-client":
|
2022-11-09 07:12:17 +00:00
|
|
|
w.Write(project4Payload)
|
2015-08-22 19:06:56 +00:00
|
|
|
return
|
2017-08-10 06:24:35 +00:00
|
|
|
case "/api/v4/projects/brightbox/puppet":
|
2024-02-05 23:10:23 +00:00
|
|
|
case "/api/v4/projects/6":
|
2022-11-09 07:12:17 +00:00
|
|
|
w.Write(project6Payload)
|
2016-01-07 14:35:53 +00:00
|
|
|
return
|
2021-12-19 00:12:09 +00:00
|
|
|
case "/api/v4/projects/4/hooks":
|
2015-08-22 19:06:56 +00:00
|
|
|
switch r.Method {
|
2021-12-19 00:12:09 +00:00
|
|
|
case "GET":
|
|
|
|
w.Write(project4PayloadHooks)
|
|
|
|
case "POST":
|
|
|
|
w.Write(project4PayloadHook)
|
2015-08-22 19:06:56 +00:00
|
|
|
w.WriteHeader(201)
|
|
|
|
}
|
2021-12-19 00:12:09 +00:00
|
|
|
return
|
|
|
|
case "/api/v4/projects/4/hooks/10717088":
|
|
|
|
w.WriteHeader(201)
|
2015-08-22 19:06:56 +00:00
|
|
|
return
|
2024-02-05 23:10:23 +00:00
|
|
|
case "/api/v4/projects/4/members/all/3":
|
|
|
|
w.Write(project4PayloadMembers)
|
|
|
|
return
|
|
|
|
case "/api/v4/projects/diaspora/diaspora-client/members/all/3":
|
|
|
|
w.Write(project4PayloadMembers)
|
|
|
|
return
|
|
|
|
case "/api/v4/projects/6/members/all/3":
|
|
|
|
w.Write(project6PayloadMembers)
|
|
|
|
return
|
2015-08-22 19:06:56 +00:00
|
|
|
case "/oauth/token":
|
|
|
|
w.Write(accessTokenPayload)
|
|
|
|
return
|
2017-08-10 06:24:35 +00:00
|
|
|
case "/api/v4/user":
|
2016-01-07 14:35:53 +00:00
|
|
|
w.Write(currentUserPayload)
|
2015-08-22 19:06:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// else return a 404
|
|
|
|
http.NotFound(w, r)
|
|
|
|
})
|
|
|
|
|
|
|
|
// return the server to the client which
|
|
|
|
// will need to know the base URL path
|
|
|
|
return server
|
|
|
|
}
|