1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-12 08:23:48 +02:00
woodpecker/server/remote/gitlab/testdata/testdata.go

89 lines
2.3 KiB
Go
Raw Normal View History

2018-02-20 00:24:10 +02:00
// Copyright 2018 Drone.IO Inc.
2019-09-14 14:21:16 +02:00
//
2018-02-20 00:24:10 +02: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 14:21:16 +02:00
//
2018-02-20 00:24:10 +02:00
// http://www.apache.org/licenses/LICENSE-2.0
2019-09-14 14:21:16 +02:00
//
2018-02-20 00:24:10 +02: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 21:06:56 +02:00
package testdata
import (
"encoding/json"
"io"
2015-08-22 21:06:56 +02:00
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
2015-08-22 21:06:56 +02:00
)
// NewServer setup a mock server for testing purposes.
func NewServer(t *testing.T) *httptest.Server {
2015-08-22 21:06:56 +02:00
mux := http.NewServeMux()
server := httptest.NewServer(mux)
// handle requests and serve mock data
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
t.Logf("gitlab remote mock server: [%s] %s", r.Method, r.URL.Path)
2015-08-22 21:06:56 +02:00
// evaluate the path to serve a dummy data file
// 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 21:06:56 +02:00
switch r.URL.Path {
2017-08-10 08:24:35 +02:00
case "/api/v4/projects":
if r.FormValue("archived") == "false" {
w.Write(notArchivedProjectsPayload)
} else {
w.Write(allProjectsPayload)
}
2015-08-22 21:06:56 +02:00
return
2017-08-10 08:24:35 +02:00
case "/api/v4/projects/diaspora/diaspora-client":
2015-08-22 21:06:56 +02:00
w.Write(project4Paylod)
return
2017-08-10 08:24:35 +02:00
case "/api/v4/projects/brightbox/puppet":
2016-01-07 16:35:53 +02:00
w.Write(project6Paylod)
return
case "/api/v4/projects/4/services/drone-ci":
2015-08-22 21:06:56 +02:00
switch r.Method {
case "PUT":
body, _ := io.ReadAll(r.Body)
opts := make(map[string]interface{})
assert.NoError(t, json.Unmarshal(body, &opts))
token, ok := opts["token"].(string)
assert.True(t, ok)
if token == "" {
2015-08-22 21:06:56 +02:00
w.WriteHeader(404)
} else {
w.WriteHeader(201)
}
case "DELETE":
w.WriteHeader(201)
}
return
case "/oauth/token":
w.Write(accessTokenPayload)
return
2017-08-10 08:24:35 +02:00
case "/api/v4/user":
2016-01-07 16:35:53 +02:00
w.Write(currentUserPayload)
2015-08-22 21:06:56 +02: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
}