MCPcopy
hub / github.com/elastic/go-elasticsearch / TestAPIRequest

Function TestAPIRequest

esapi/esapi_request_internal_test.go:30–81  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

28)
29
30func TestAPIRequest(t *testing.T) {
31 var (
32 body string
33 req *http.Request
34 err error
35 )
36
37 t.Run("newRequest", func(t *testing.T) {
38 req, err = newRequest("GET", "/foo", nil)
39 if err != nil {
40 t.Fatalf("Unexpected error: %s", err)
41 }
42
43 if req.Method != "GET" {
44 t.Errorf("Unexpected method %s, want GET", req.Method)
45 }
46 if req.URL.String() != "/foo" {
47 t.Errorf("Unexpected URL %s, want /foo", req.URL)
48 }
49
50 body = `{"foo":"bar"}`
51 req, err = newRequest("GET", "/foo", strings.NewReader(body))
52 if err != nil {
53 t.Fatalf("Unexpected error: %s", err)
54 }
55
56 if int(req.ContentLength) != len(body) {
57 t.Errorf("Unexpected length of req.Body, got=%d, want=%d", req.ContentLength, len(body))
58 }
59
60 req, err = newRequest("GET", "/foo", bytes.NewBuffer([]byte(body)))
61 if err != nil {
62 t.Fatalf("Unexpected error: %s", err)
63 }
64
65 req, err = newRequest("GET", "/foo", bytes.NewReader([]byte(body)))
66 if err != nil {
67 t.Fatalf("Unexpected error: %s", err)
68 }
69
70 req, err = newRequest("GET", "http:////_aliases/test", nil)
71 if err != nil {
72 t.Fatalf("Unexpected error: %s", err)
73 }
74 if req.URL.String() != "http:////_aliases/test" {
75 t.Errorf("Unexpected url for request: %s", req.URL.String())
76 }
77 if req.URL.Host != "" {
78 t.Errorf("Unexpected host, should be empty, got: %s", req.URL.Host)
79 }
80 })
81}

Callers

nothing calls this directly

Calls 3

newRequestFunction · 0.85
RunMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected