-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint_test.go
48 lines (39 loc) · 1.08 KB
/
endpoint_test.go
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestFetchQuote(t *testing.T) {
expectedBytes := []byte(`{"quote":"This is a quote","author":"Quotey McQuoteface"}`)
expectedQuote := Quote{
Text: "This is a quote",
Author: "Quotey McQuoteface",
}
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write(expectedBytes)
}))
defer server.Close()
endpoint := Endpoint{URL: server.URL, Client: server.Client()}
quote, err := endpoint.FetchQuote()
if err != nil {
t.Errorf("Error fetching quote: %e", err)
}
if quote.Text != expectedQuote.Text || quote.Author != expectedQuote.Author {
t.Errorf("Quote does not match. Expected: %+v. Received: %+v.", expectedQuote, quote)
}
}
func TestGetPortFromString(t *testing.T) {
var i interface{} = "8080"
port, _ := GetPort(i)
if port != 8080 {
t.Errorf("Parsing port from string failed")
}
}
func TestGetPortFromFloat64(t *testing.T) {
var i interface{} = float64(8080)
port, _ := GetPort(i)
if port != 8080 {
t.Errorf("Parsing port from string failed")
}
}