github.com/crosbymichael/octokat@v0.0.0-20160826194511-076a32289ed5/octokat_test.go (about) 1 package octokat 2 3 import ( 4 "fmt" 5 "github.com/bmizerany/assert" 6 "io/ioutil" 7 "net/http" 8 "net/http/httptest" 9 "os" 10 "testing" 11 ) 12 13 var ( 14 // mux is the HTTP request multiplexer used with the test server. 15 mux *http.ServeMux 16 17 // client is the GitHub client being tested. 18 client *Client 19 20 // server is a test HTTP server used to provide mock API responses. 21 server *httptest.Server 22 ) 23 24 // setup sets up a test HTTP server along with a octokat.Client that is 25 // configured to talk to that test server. Tests should register handlers on 26 // mux which provide mock responses for the API method being tested. 27 func setup() { 28 // test server 29 mux = http.NewServeMux() 30 server = httptest.NewServer(mux) 31 32 // octokat client configured to use test server 33 client = NewClient() 34 client.BaseURL = server.URL 35 } 36 37 // teardown closes the test HTTP server. 38 func tearDown() { 39 server.Close() 40 } 41 42 func testMethod(t *testing.T, r *http.Request, want string) { 43 assert.Equal(t, want, r.Method) 44 } 45 46 func testHeader(t *testing.T, r *http.Request, header string, want string) { 47 assert.Equal(t, want, r.Header.Get(header)) 48 } 49 50 func testBody(t *testing.T, r *http.Request, want string) { 51 body, _ := ioutil.ReadAll(r.Body) 52 assert.Equal(t, want, string(body)) 53 } 54 55 func respondWith(w http.ResponseWriter, s string) { 56 fmt.Fprint(w, s) 57 } 58 59 func testURLOf(path string) string { 60 return fmt.Sprintf("%s/%s", server.URL, path) 61 } 62 63 func loadFixture(f string) string { 64 pwd, _ := os.Getwd() 65 p := fmt.Sprintf("%s/fixtures/%s", pwd, f) 66 c, _ := ioutil.ReadFile(p) 67 return string(c) 68 }