Commit d5898459 authored by Martin Sefcik's avatar Martin Sefcik

Used local instead of global vars

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