-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
99 lines (83 loc) · 2.63 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package corenlp
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"google.golang.org/protobuf/proto"
)
func TestNew(t *testing.T) {
t.Run("invalid address", func(t *testing.T) {
_, err := New("invalid-address", http.DefaultClient)
var uErr *url.Error
t.Logf("Returned error: (Type: %T) %v", err, err)
if !errors.As(err, &uErr) {
t.Error("Returned error is not a URL error")
}
})
t.Run("successful creation", func(t *testing.T) {
client, err := New("http://127.0.0.1:9000", http.DefaultClient)
if err != nil {
t.Errorf("Expected err to be nil, got: %v", err)
}
if client == nil {
t.Error("Returned client is nil")
}
})
}
func MustMarshalMessage(t testing.TB, msg proto.Message) []byte {
b, err := marshalMessage(msg)
if err != nil {
t.Fatalf("failed to encode message: %v", err)
}
return b
}
func TestClient_Annotate(t *testing.T) {
t.Run("successful", func(t *testing.T) {
text := "the quick brown fox jumps over the lazy dog"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
doc := &Document{Text: &text}
encodedMessage := MustMarshalMessage(t, doc)
if _, err := w.Write(encodedMessage); err != nil {
t.Fatalf("failed to write response: %v", err)
}
}))
defer ts.Close()
client, err := New(ts.URL, ts.Client())
if err != nil {
t.Fatalf("Failed to create the client: %v", err)
}
if _, err := client.Annotate(context.Background(), text, []string{"tokenize", "ssplit", "pos"}); err != nil {
t.Fatalf("Failed to annotate text: %v", err)
}
})
t.Run("server error", func(t *testing.T) {
t.Run("validation error", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
if _, err := w.Write([]byte("something failed")); err != nil {
t.Fatalf("failed to write response: %v", err)
}
}))
defer ts.Close()
client, err := New(ts.URL, ts.Client())
if err != nil {
t.Fatalf("Failed to create the client: %v", err)
}
_, err = client.Annotate(context.Background(), "", []string{"tokenize", "ssplit", "pos"})
var serr *ServerError
if !errors.As(err, &serr) {
t.Fatalf("Expected a ServerError to be returned, got: (Type: %T) %v", err, err)
}
if serr.statusCode != http.StatusInternalServerError {
t.Errorf("Expected error to have statusCode %d, got %d", http.StatusInternalServerError, serr.statusCode)
}
if serr.body != "something failed" {
t.Errorf("Expected error to have body '%s', got '%s'", "something failed", serr.body)
}
t.Log(serr.Error())
})
})
}