139 lines
3.3 KiB
Go
139 lines
3.3 KiB
Go
|
package client
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"net/http/httptest"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func testServer(expectedkey string) *httptest.Server {
|
||
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
if r.URL.Query().Get("bibkeys") == expectedkey {
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
w.Write([]byte(fmt.Sprintf(exampleResponse, expectedkey)))
|
||
|
} else {
|
||
|
w.WriteHeader(http.StatusNotFound)
|
||
|
}
|
||
|
}))
|
||
|
}
|
||
|
|
||
|
func TestFetch(t *testing.T) {
|
||
|
r := testServer("ISBN:9780980200447")
|
||
|
defer r.Close()
|
||
|
|
||
|
c := Client{}
|
||
|
c.SetAPIPath(r.URL)
|
||
|
b, err := c.GetByISBN("9780980200447")
|
||
|
if err != nil {
|
||
|
t.Log(err)
|
||
|
t.Fail()
|
||
|
}
|
||
|
if b.Identifiers.Google[0] != "4LQU1YwhY6kC" {
|
||
|
t.Logf("wrong identifier in response: %s", b.Identifiers.Google[0])
|
||
|
t.Fail()
|
||
|
}
|
||
|
}
|
||
|
func TestFetchFailure(t *testing.T) {
|
||
|
r := testServer("ISBN:1234567890")
|
||
|
defer r.Close()
|
||
|
|
||
|
c := Client{}
|
||
|
c.SetAPIPath(r.URL)
|
||
|
_, err := c.GetByISBN("9780980200447")
|
||
|
if err == nil {
|
||
|
t.Log("expected error, received nil")
|
||
|
t.Fail()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var exampleResponse = `{
|
||
|
"%s": {
|
||
|
"publishers": [
|
||
|
{
|
||
|
"name": "Litwin Books"
|
||
|
}
|
||
|
],
|
||
|
"identifiers": {
|
||
|
"google": [
|
||
|
"4LQU1YwhY6kC"
|
||
|
],
|
||
|
"lccn": [
|
||
|
"2008054742"
|
||
|
],
|
||
|
"isbn_13": [
|
||
|
"9780980200447"
|
||
|
],
|
||
|
"amazon": [
|
||
|
"098020044X"
|
||
|
],
|
||
|
"isbn_10": [
|
||
|
"1234567890"
|
||
|
],
|
||
|
"oclc": [
|
||
|
"297222669"
|
||
|
],
|
||
|
"librarything": [
|
||
|
"8071257"
|
||
|
],
|
||
|
"project_gutenberg": [
|
||
|
"14916"
|
||
|
],
|
||
|
"goodreads": [
|
||
|
"6383507"
|
||
|
]
|
||
|
},
|
||
|
"classifications": {
|
||
|
"dewey_decimal_class": [
|
||
|
"028/.9"
|
||
|
],
|
||
|
"lc_classifications": [
|
||
|
"Z1003 .M58 2009"
|
||
|
]
|
||
|
},
|
||
|
"links": [
|
||
|
{
|
||
|
"url": "http://johnmiedema.ca",
|
||
|
"title": "Author's Website"
|
||
|
}
|
||
|
],
|
||
|
"weight": "1 grams",
|
||
|
"title": "Slow reading",
|
||
|
"url": "https://openlibrary.org/books/OL22853304M/Slow_reading",
|
||
|
"number_of_pages": 80,
|
||
|
"cover": {
|
||
|
"small": "https://covers.openlibrary.org/b/id/5546156-S.jpg",
|
||
|
"large": "https://covers.openlibrary.org/b/id/5546156-L.jpg",
|
||
|
"medium": "https://covers.openlibrary.org/b/id/5546156-M.jpg"
|
||
|
},
|
||
|
"subjects": [
|
||
|
{
|
||
|
"url": "https://openlibrary.org/subjects/books_and_reading",
|
||
|
"name": "Books and reading"
|
||
|
},
|
||
|
{
|
||
|
"url": "https://openlibrary.org/subjects/reading",
|
||
|
"name": "Reading"
|
||
|
}
|
||
|
],
|
||
|
"publish_date": "2009",
|
||
|
"authors": [
|
||
|
{
|
||
|
"url": "https://openlibrary.org/authors/OL6548935A/John_Miedema",
|
||
|
"name": "John Miedema"
|
||
|
}
|
||
|
],
|
||
|
"excerpts": [
|
||
|
{
|
||
|
"comment": "test purposes",
|
||
|
"text": "test first page"
|
||
|
}
|
||
|
],
|
||
|
"publish_places": [
|
||
|
{
|
||
|
"name": "Duluth, Minn"
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
}`
|