github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/opensearchapi_request_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  	"bytes"
    33  	"io"
    34  	"net/http"
    35  	"strings"
    36  	"testing"
    37  )
    38  
    39  func TestAPIRequest(t *testing.T) {
    40  	var (
    41  		body string
    42  		req  *http.Request
    43  		err  error
    44  	)
    45  
    46  	t.Run("newRequest", func(t *testing.T) {
    47  		req, err = newRequest("GET", "/foo", nil)
    48  		if err != nil {
    49  			t.Fatalf("Unexpected error: %s", err)
    50  		}
    51  
    52  		if req.Method != "GET" {
    53  			t.Errorf("Unexpected method %s, want GET", req.Method)
    54  		}
    55  		if req.URL.String() != "/foo" {
    56  			t.Errorf("Unexpected URL %s, want /foo", req.URL)
    57  		}
    58  
    59  		body = `{"foo":"bar"}`
    60  		req, err = newRequest("GET", "/foo", strings.NewReader(body))
    61  		if err != nil {
    62  			t.Fatalf("Unexpected error: %s", err)
    63  		}
    64  
    65  		if _, ok := req.Body.(io.ReadCloser); !ok {
    66  			t.Errorf("Unexpected type for req.Body: %T", req.Body)
    67  		}
    68  		if int(req.ContentLength) != len(body) {
    69  			t.Errorf("Unexpected length of req.Body, got=%d, want=%d", req.ContentLength, len(body))
    70  		}
    71  
    72  		req, err = newRequest("GET", "/foo", bytes.NewBuffer([]byte(body)))
    73  		if err != nil {
    74  			t.Fatalf("Unexpected error: %s", err)
    75  		}
    76  		if _, ok := req.Body.(io.ReadCloser); !ok {
    77  			t.Errorf("Unexpected type for req.Body: %T", req.Body)
    78  		}
    79  		req, err = newRequest("GET", "/foo", bytes.NewReader([]byte(body)))
    80  		if err != nil {
    81  			t.Fatalf("Unexpected error: %s", err)
    82  		}
    83  		if _, ok := req.Body.(io.ReadCloser); !ok {
    84  			t.Errorf("Unexpected type for req.Body: %T", req.Body)
    85  		}
    86  	})
    87  }