Skip to content

Instantly share code, notes, and snippets.

@gongo
Created August 7, 2014 04:06
Show Gist options
  • Save gongo/aed5ca622aad00f3a18f to your computer and use it in GitHub Desktop.
Save gongo/aed5ca622aad00f3a18f to your computer and use it in GitHub Desktop.
httptest お試し
package airplay
import (
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"
)
func TestStop(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/stop" || req.Method != "POST" {
t.Errorf("client.Stop() request is not 'POST /stop' (actual = %s %s)", req.Method, req.URL.Path)
w.WriteHeader(400)
return
}
}))
addr, port := getAddrAndPort(ts.URL)
device := Device{
Addr: addr,
Port: port,
}
client, _ := NewClientHasDevice(device)
client.Stop()
}
func getAddrAndPort(host string) (string, int) {
u, _ := url.Parse(host)
split := strings.Split(u.Host, ":")
addr := split[0]
port, _ := strconv.Atoi(split[1])
return addr, port
}
// If:
// func (c *Client) Stop() {
// c.connection.post("stop", nil)
// }
//
// $ go test
// PASS
// ok github.com/gongo/go-airplay 0.026s
// If:
// func (c *Client) Stop() {
// c.connection.post("start", nil)
// }
//
// --- FAIL: TestStop (0.00 seconds)
// client_test.go:15: client.Stop() request is not 'POST /stop' (actual = POST /start)
// FAIL
// exit status 1
// FAIL github.com/gongo/go-airplay 0.026s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment