204 lines
4.9 KiB
Go
204 lines
4.9 KiB
Go
package client
|
|
|
|
import (
|
|
"errors"
|
|
"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") == "SPECIAL:MISSING" {
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte("{}"))
|
|
} else 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 || !errors.Is(err, NetworkError) {
|
|
t.Logf("expected error, received nil: %v", err)
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestBookNotFound(t *testing.T) {
|
|
r := testServer("")
|
|
defer r.Close()
|
|
|
|
c := Client{}
|
|
c.SetAPIPath(r.URL)
|
|
_, err := c.GetByRawKey("SPECIAL:MISSING")
|
|
if err == nil || !errors.Is(err, NotFoundError) {
|
|
t.Logf("expected error, received nil: %v", err)
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestKeyRejections(t *testing.T) {
|
|
c := Client{}
|
|
cases := map[string][]string{
|
|
"ISBN": {"", "1", "12", "123", "1234", "12345", "123456", "1234567", "12345678", "123456789", "12345678901", "123456789012", "12345678901234"},
|
|
"OCLC": {""},
|
|
"LCCN": {""},
|
|
"OLID": {""},
|
|
}
|
|
for id, ts := range cases {
|
|
switch id {
|
|
case "ISBN":
|
|
for i := range ts {
|
|
_, err := c.GetByISBN(ts[i])
|
|
if !errors.Is(err, FormatError) {
|
|
t.Logf("%s received %v, expected %v", id, err, FormatError)
|
|
t.Fail()
|
|
}
|
|
}
|
|
case "OCLC":
|
|
for i := range ts {
|
|
_, err := c.GetByOCLC(ts[i])
|
|
if !errors.Is(err, FormatError) {
|
|
t.Logf("%s received %v, expected %v", id, err, FormatError)
|
|
t.Fail()
|
|
}
|
|
}
|
|
case "LCCN":
|
|
for i := range ts {
|
|
_, err := c.GetByLCCN(ts[i])
|
|
if !errors.Is(err, FormatError) {
|
|
t.Logf("%s received %v, expected %v", id, err, FormatError)
|
|
t.Fail()
|
|
}
|
|
}
|
|
case "OLID":
|
|
for i := range ts {
|
|
_, err := c.GetByOLID(ts[i])
|
|
if !errors.Is(err, FormatError) {
|
|
t.Logf("%s received %v, expected %v", id, err, FormatError)
|
|
t.Fail()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// example comes from the official API docs
|
|
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"
|
|
}
|
|
]
|
|
}
|
|
}`
|