github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/opensearchapi_response_internal_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // 3 // The OpenSearch Contributors require contributions made to 4 // this file be licensed under the Apache-2.0 license or a 5 // compatible open source license. 6 // 7 // Modifications Copyright OpenSearch Contributors. See 8 // GitHub history for details. 9 10 // Licensed to Elasticsearch B.V. under one or more contributor 11 // license agreements. See the NOTICE file distributed with 12 // this work for additional information regarding copyright 13 // ownership. Elasticsearch B.V. licenses this file to you under 14 // the Apache License, Version 2.0 (the "License"); you may 15 // not use this file except in compliance with the License. 16 // You may obtain a copy of the License at 17 // 18 // http://www.apache.org/licenses/LICENSE-2.0 19 // 20 // Unless required by applicable law or agreed to in writing, 21 // software distributed under the License is distributed on an 22 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 23 // KIND, either express or implied. See the License for the 24 // specific language governing permissions and limitations 25 // under the License. 26 27 // +build !integration 28 29 package opensearchapi 30 31 import ( 32 "errors" 33 "io/ioutil" 34 "net/http" 35 "strings" 36 "testing" 37 ) 38 39 type errReader struct{} 40 41 func (errReader) Read(p []byte) (n int, err error) { return 1, errors.New("MOCK ERROR") } 42 43 func TestAPIResponse(t *testing.T) { 44 var ( 45 body string 46 res *Response 47 ) 48 49 t.Run("String", func(t *testing.T) { 50 body = `{"foo":"bar"}` 51 52 res = &Response{StatusCode: 200, Body: ioutil.NopCloser(strings.NewReader(body))} 53 54 expected := `[200 OK]` + ` ` + body 55 if res.String() != expected { 56 t.Errorf("Unexpected response: %s, want: %s", res.String(), expected) 57 } 58 }) 59 60 t.Run("String with empty response", func(t *testing.T) { 61 res = &Response{} 62 63 if res.String() != "[0 ]" { 64 t.Errorf("Unexpected response: %s", res.String()) 65 } 66 }) 67 68 t.Run("String with nil response", func(t *testing.T) { 69 res = nil 70 71 if res.String() != "[0 <nil>]" { 72 t.Errorf("Unexpected response: %s", res.String()) 73 } 74 }) 75 76 t.Run("String Error", func(t *testing.T) { 77 res = &Response{StatusCode: 200, Body: ioutil.NopCloser(errReader{})} 78 79 if !strings.Contains(res.String(), `error reading response`) { 80 t.Errorf("Expected response string to contain 'error reading response', got: %s", res.String()) 81 } 82 }) 83 84 t.Run("Status", func(t *testing.T) { 85 res = &Response{StatusCode: 404} 86 87 if res.Status() != `404 Not Found` { 88 t.Errorf("Unexpected response status text: %s, want: 404 Not Found", res.Status()) 89 } 90 }) 91 92 t.Run("IsError", func(t *testing.T) { 93 res = &Response{StatusCode: 201} 94 95 if res.IsError() { 96 t.Errorf("Unexpected error for response: %s", res.Status()) 97 } 98 99 res = &Response{StatusCode: 403} 100 101 if !res.IsError() { 102 t.Errorf("Expected error for response: %s", res.Status()) 103 } 104 }) 105 106 t.Run("Warnings", func(t *testing.T) { 107 hdr := http.Header{} 108 hdr.Add("Warning", "Foo 1") 109 hdr.Add("Warning", "Foo 2") 110 res = &Response{StatusCode: 201, Header: hdr} 111 112 if !res.HasWarnings() { 113 t.Errorf("Expected response to have warnings") 114 } 115 116 if len(res.Warnings()) != 2 { 117 t.Errorf("Expected [2] warnings, got: %d", len(res.Warnings())) 118 } 119 }) 120 }