60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package scryfall
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
const TEST_ID = "56ebc372-aabd-4174-a943-c7bf59e5028d"
|
|
const TEST_ENDPOINT = "/cards/" + TEST_ID
|
|
const TESTDATA_DIRECTORY = "testdata/"
|
|
const TEST_GOOD_CARD_DATA_FILENAME = "card.json"
|
|
|
|
var (
|
|
mux *http.ServeMux
|
|
server *httptest.Server
|
|
client *ScryfallClient
|
|
)
|
|
|
|
func setup() func() {
|
|
mux = http.NewServeMux()
|
|
server = httptest.NewServer(mux)
|
|
|
|
client, _ = NewScryfallClient(BaseURL(server.URL))
|
|
|
|
return func() {
|
|
server.Close()
|
|
}
|
|
}
|
|
|
|
func fixture(filename string) string {
|
|
bytes, err := ioutil.ReadFile(TESTDATA_DIRECTORY + filename)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return string(bytes)
|
|
}
|
|
|
|
func Test_GetCardImageUrlById_ReturnsUrl_ForSuccessfulRequest(t *testing.T) {
|
|
teardown := setup()
|
|
defer teardown()
|
|
|
|
mux.HandleFunc(TEST_ENDPOINT, func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
fmt.Fprintf(w, fixture(TEST_GOOD_CARD_DATA_FILENAME))
|
|
})
|
|
|
|
url, err := client.GetCardImageUrlById(TEST_ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if url != "https://cards.scryfall.io/png/front/5/6/56ebc372-aabd-4174-a943-c7bf59e5028d.png?1562629953" {
|
|
t.Fatal(err)
|
|
}
|
|
}
|