~kota/metweather

ref: c166ea977f8e60ea993cbf9320f87b8226d45934 metweather/cmd/root_test.go -rw-r--r-- 860 bytes
c166ea97Dakota Walsh fix location help message 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package cmd

import (
	"net/http"
	"net/http/httptest"
	"net/url"

	"git.sr.ht/~kota/metservice-go"
)

// setup sets up a test HTTP server along with a 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() (client *metservice.Client, mux *http.ServeMux, teardown func()) {
	// mux is the HTTP request multiplexer used with the test server.
	mux = http.NewServeMux()

	// server is a test HTTP server used to provide mock API responses.
	server := httptest.NewServer(mux)

	// client is the Client being tested and is configured to use test server.
	localURL, _ := url.Parse(server.URL + "/")
	localClient := &metservice.Client{
		HTTPClient: http.DefaultClient,
		BaseURL:    localURL.String(),
	}

	return localClient, mux, server.Close
}