Commit d5898459 authored by Martin Sefcik's avatar Martin Sefcik

Used local instead of global vars

parent 30b9d9e6
......@@ -10,32 +10,25 @@ import (
"testing"
)
var (
// mux is the HTTP request multiplexer used with the test server.
mux *http.ServeMux
// client is the Gitlab client being tested.
client *Client
// server is a test HTTP server used to provide mock API responses.
server *httptest.Server
)
// setup sets up a test HTTP server along with a gitlab.Client that is
// 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() {
// test server
mux = http.NewServeMux()
server = httptest.NewServer(mux)
func setup() (*http.ServeMux, *httptest.Server, *Client) {
// mux is the HTTP request multiplexer used with the test server.
mux := http.NewServeMux()
// gitlab client configured to use test server
client = NewClient(nil, "")
// server is a test HTTP server used to provide mock API responses.
server := httptest.NewServer(mux)
// client is the Gitlab client being tested.
client := NewClient(nil, "")
client.SetBaseURL(server.URL)
return mux, server, client
}
// teardown closes the test HTTP server.
func teardown() {
func teardown(server *httptest.Server) {
server.Close()
}
......@@ -112,4 +105,4 @@ func TestCheckResponse(t *testing.T) {
if !reflect.DeepEqual(err, want) {
t.Errorf("Error = %#v, want %#v", err, want)
}
}
\ No newline at end of file
}
......@@ -8,8 +8,8 @@ import (
)
func TestListProjects(t *testing.T) {
setup()
defer teardown()
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
......@@ -39,8 +39,8 @@ func TestListProjects(t *testing.T) {
}
func TestListOwnedProjects(t *testing.T) {
setup()
defer teardown()
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/owned", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
......@@ -70,8 +70,8 @@ func TestListOwnedProjects(t *testing.T) {
}
func TestListAllProjects(t *testing.T) {
setup()
defer teardown()
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/all", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
......@@ -101,8 +101,8 @@ func TestListAllProjects(t *testing.T) {
}
func TestGetProject(t *testing.T) {
setup()
defer teardown()
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
......@@ -122,8 +122,8 @@ func TestGetProject(t *testing.T) {
}
func TestSearchProjects(t *testing.T) {
setup()
defer teardown()
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/search/query", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
......@@ -150,8 +150,8 @@ func TestSearchProjects(t *testing.T) {
}
func TestCreateProject(t *testing.T) {
setup()
defer teardown()
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
......
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