This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
unpaywall_test.go
81 lines (70 loc) · 2.19 KB
/
unpaywall_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main_test
import (
"net/http"
"os"
"strings"
"testing"
"github.com/go-test/deep"
pass "github.com/oa-pass/pass-download-service"
)
func TestUnpaywall(t *testing.T) {
doi := "test/foo"
email := "[email protected]"
baseuri := "http://example.org/unpaywall/v2"
// Based on things in real_response.json
expected := &pass.DoiInfo{
Manuscripts: []pass.Manuscript{
{
Location: "https://dash.harvard.edu/bitstream/1/12285462/1/Nanometer-Scale%20Thermometry.pdf",
RepositoryInstitution: "Harvard University - Digital Access to Scholarship at Harvard (DASH)",
Type: "application/pdf",
Source: "Unpaywall",
Name: "Nanometer-Scale Thermometry.pdf",
},
{
Location: "http://europepmc.org/articles/pmc4221854?pdf=render",
RepositoryInstitution: "pubmedcentral.nih.gov",
Type: "application/pdf",
Source: "Unpaywall",
Name: "pmc4221854?pdf=render",
},
{
Location: "http://arxiv.org/pdf/1304.1068",
RepositoryInstitution: "arXiv.org",
Type: "application/pdf",
Source: "Unpaywall",
Name: "1304.1068",
},
},
}
file, err := os.Open("testdata/real_response.json")
if err != nil {
t.Fatalf("Could not open test response: %v", err)
}
toTest := pass.UnpaywallService{
HTTP: MockRequester(func(req *http.Request) (*http.Response, error) {
expectedURI := baseuri + "/" + doi + "?email=" + email
if req.URL.String() != expectedURI {
t.Fatalf("Did not get expected unpaywall request URL. Expected: %s, got: %s", expectedURI, req.URL.String())
}
if req.Method != http.MethodGet {
t.Fatalf("Expected GET method, got %s", req.Method)
}
return &http.Response{
StatusCode: 200,
Body: file,
}, nil
}),
Email: email,
Baseuri: baseuri,
Cache: pass.NewDoiCache(pass.DoiCacheConfig{}),
}
result, err := toTest.Lookup(doi)
if err != nil {
t.Fatalf("Lookup failed: %v", err)
}
diffs := deep.Equal(result, expected)
if len(diffs) > 0 {
t.Fatalf("result differed from expected %s", strings.Join(diffs, "\n"))
}
}