forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
45 lines (40 loc) · 1.1 KB
/
errors_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
package elastic
import (
"bufio"
"fmt"
"net/http"
"strings"
"testing"
)
func TestResponseError(t *testing.T) {
message := "Something went seriously wrong."
raw := "HTTP/1.1 500 Internal Server Error\r\n" +
"\r\n" +
`{"status":500,"error":"` + message + `"}` + "\r\n"
r := bufio.NewReader(strings.NewReader(raw))
resp, err := http.ReadResponse(r, nil)
if err != nil {
t.Fatal(err)
}
err = checkResponse(resp)
if err == nil {
t.Fatalf("expected error; got: %v", err)
}
// Check for correct error message
expected := fmt.Sprintf("elastic: Error %d (%s): %s", resp.StatusCode, http.StatusText(resp.StatusCode), message)
got := err.Error()
if got != expected {
t.Fatalf("expected %q; got: %q", expected, got)
}
// Check that error is of type *elastic.Error, which contains additional information
e, ok := err.(*Error)
if !ok {
t.Fatal("expected error to be of type *elastic.Error")
}
if e.Status != resp.StatusCode {
t.Fatalf("expected status code %d; got: %d", resp.StatusCode, e.Status)
}
if e.Message != message {
t.Fatalf("expected error message %q; got: %q", message, e.Message)
}
}