-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_test.go
64 lines (56 loc) · 1.53 KB
/
client_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
package paperswithcode_go
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWithAPIToken(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("ok"))
assert.NoError(t, err)
}))
defer server.Close()
c := NewClient(WithAPIToken("MY_TOKEN"))
emptyRequest, err := http.NewRequest(http.MethodGet, server.URL, nil)
assert.NoError(t, err)
_, err = c.httpClient.Transport.RoundTrip(emptyRequest)
assert.NoError(t, err)
assert.Equal(t, "Token MY_TOKEN", emptyRequest.Header.Get("Authorization"))
}
func TestTransportIsNotProvidedWhenNoAPIIsProvided(t *testing.T) {
c := NewClient()
assert.Nil(t, c.httpClient.Transport)
}
func ExampleGetPaperIDFromPaperTitle() {
paperTitle := "Generative Adversarial Networks"
fmt.Println(GetPaperIDFromPaperTitle(paperTitle))
// Output: generative-adversarial-networks
}
func TestGetPaperIDFromPaperTitle(t *testing.T) {
tests := []struct {
paperTitle string
wantPaperID string
}{
{
paperTitle: "This Is Paper",
wantPaperID: "this-is-paper",
},
{
paperTitle: "This Is Paper",
wantPaperID: "this-is-paper",
},
{
paperTitle: "This-Is-Paper",
wantPaperID: "this-is-paper",
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("The ID of %s should be %s", tt.paperTitle, tt.wantPaperID),
func(t *testing.T) {
assert.Equal(t, tt.wantPaperID, GetPaperIDFromPaperTitle(tt.paperTitle))
})
}
}