github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchtransport/opensearchtransport_integration_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  //go:build integration
    28  // +build integration
    29  
    30  package opensearchtransport_test
    31  
    32  import (
    33  	"bytes"
    34  	"fmt"
    35  	"io"
    36  	"io/ioutil"
    37  	"net/http"
    38  	"net/http/httptest"
    39  	"net/url"
    40  	"strings"
    41  	"testing"
    42  
    43  	"github.com/opensearch-project/opensearch-go/v2/opensearchtransport"
    44  	"github.com/opensearch-project/opensearch-go/v2/opensearchutil"
    45  )
    46  
    47  var (
    48  	_ = fmt.Print
    49  )
    50  
    51  func TestTransportRetries(t *testing.T) {
    52  	var counter int
    53  
    54  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    55  		counter++
    56  
    57  		body, _ := ioutil.ReadAll(r.Body)
    58  		fmt.Println("req.Body:", string(body))
    59  
    60  		http.Error(w, "FAKE 502", http.StatusBadGateway)
    61  	}))
    62  	serverURL, _ := url.Parse(server.URL)
    63  
    64  	transport, _ := opensearchtransport.New(opensearchtransport.Config{URLs: []*url.URL{serverURL}})
    65  
    66  	bodies := []io.Reader{
    67  		strings.NewReader(`FAKE`),
    68  		opensearchutil.NewJSONReader(`FAKE`),
    69  	}
    70  
    71  	for _, body := range bodies {
    72  		t.Run(fmt.Sprintf("Reset the %T request body", body), func(t *testing.T) {
    73  			counter = 0
    74  
    75  			req, err := http.NewRequest("GET", "/", body)
    76  			if err != nil {
    77  				t.Fatalf("Unexpected error: %s", err)
    78  			}
    79  
    80  			res, err := transport.Perform(req)
    81  			if err != nil {
    82  				t.Fatalf("Unexpected error: %s", err)
    83  			}
    84  
    85  			body, _ := ioutil.ReadAll(res.Body)
    86  
    87  			fmt.Println("> GET", req.URL)
    88  			fmt.Printf("< %s (tries: %d)\n", bytes.TrimSpace(body), counter)
    89  
    90  			if counter != 4 {
    91  				t.Errorf("Unexpected number of attempts, want=4, got=%d", counter)
    92  			}
    93  		})
    94  	}
    95  }
    96  
    97  func TestTransportHeaders(t *testing.T) {
    98  	u, _ := url.Parse("http://localhost:9200")
    99  
   100  	hdr := http.Header{}
   101  	hdr.Set("Accept", "application/yaml")
   102  
   103  	tp, _ := opensearchtransport.New(opensearchtransport.Config{
   104  		URLs:   []*url.URL{u},
   105  		Header: hdr,
   106  	})
   107  
   108  	req, _ := http.NewRequest("GET", "/", nil)
   109  	res, err := tp.Perform(req)
   110  	if err != nil {
   111  		t.Fatalf("Unexpected error: %s", err)
   112  	}
   113  	defer res.Body.Close()
   114  
   115  	body, err := ioutil.ReadAll(res.Body)
   116  	if err != nil {
   117  		t.Fatalf("Unexpected error: %s", err)
   118  	}
   119  
   120  	if !bytes.HasPrefix(body, []byte("---")) {
   121  		t.Errorf("Unexpected response body:\n%s", body)
   122  	}
   123  }
   124  
   125  func TestTransportBodyClose(t *testing.T) {
   126  	u, _ := url.Parse("http://localhost:9200")
   127  
   128  	tp, _ := opensearchtransport.New(opensearchtransport.Config{
   129  		URLs: []*url.URL{u},
   130  	})
   131  
   132  	req, _ := http.NewRequest("GET", "/", nil)
   133  	res, err := tp.Perform(req)
   134  	if err != nil {
   135  		t.Fatalf("Unexpected error: %s", err)
   136  	}
   137  	if closeResp := res.Body.Close(); closeResp != nil {
   138  		t.Fatalf("Unexpected return on res.Body.Close(): %s", closeResp)
   139  	}
   140  	if closeResp := res.Body.Close(); closeResp != nil {
   141  		t.Fatalf("Unexpected return on res.Body.Close(): %s", closeResp)
   142  	}
   143  	body, err := io.ReadAll(res.Body)
   144  	if err != nil {
   145  		t.Fatalf("Failed to read the reponse body: %s", err)
   146  	}
   147  	if body == nil || len(body) == 0 {
   148  		t.Fatalf("Unexpected response body:\n%s", body)
   149  	}
   150  }
   151  
   152  func TestTransportCompression(t *testing.T) {
   153  	var req *http.Request
   154  	var res *http.Response
   155  	var err error
   156  
   157  	u, _ := url.Parse("http://localhost:9200")
   158  
   159  	transport, _ := opensearchtransport.New(opensearchtransport.Config{
   160  		URLs:                []*url.URL{u},
   161  		CompressRequestBody: true,
   162  	})
   163  
   164  	indexName := "/shiny_new_index"
   165  
   166  	req, _ = http.NewRequest(http.MethodPut, indexName, nil)
   167  	res, err = transport.Perform(req)
   168  	if err != nil {
   169  		t.Fatalf("Unexpected error, cannot create index: %v", err)
   170  	}
   171  
   172  	req, _ = http.NewRequest(http.MethodGet, indexName, nil)
   173  	res, err = transport.Perform(req)
   174  	if err != nil {
   175  		t.Fatalf("Unexpected error, cannot find index: %v", err)
   176  	}
   177  
   178  	req, _ = http.NewRequest(
   179  		http.MethodPost,
   180  		strings.Join([]string{indexName, "/_doc"}, ""),
   181  		strings.NewReader(`{"solidPayload": 1}`),
   182  	)
   183  	req.Header.Set("Content-Type", "application/json")
   184  	res, err = transport.Perform(req)
   185  	if err != nil {
   186  		t.Fatalf("Unexpected error, cannot POST payload: %v", err)
   187  	}
   188  
   189  	if res.StatusCode != http.StatusCreated {
   190  		t.Fatalf("Unexpected StatusCode, expected 201, got: %v", res.StatusCode)
   191  	}
   192  
   193  	req, _ = http.NewRequest(http.MethodDelete, indexName, nil)
   194  	_, err = transport.Perform(req)
   195  	if err != nil {
   196  		t.Fatalf("Unexpected error, cannot DELETE %s: %v", indexName, err)
   197  	}
   198  }