...

Source file src/sigs.k8s.io/release-utils/http/http_test.go

Documentation: sigs.k8s.io/release-utils/http

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package http_test
    18  
    19  import (
    20  	"bytes"
    21  	"errors"
    22  	"io"
    23  	"net/http"
    24  	"net/http/httptest"
    25  	"strings"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/require"
    29  	khttp "sigs.k8s.io/release-utils/http"
    30  	"sigs.k8s.io/release-utils/http/httpfakes"
    31  )
    32  
    33  func TestGetURLResponseSuccess(t *testing.T) {
    34  	// Given
    35  	server := httptest.NewServer(http.HandlerFunc(
    36  		func(w http.ResponseWriter, r *http.Request) {
    37  			_, err := io.WriteString(w, "")
    38  			require.Nil(t, err)
    39  		}))
    40  	defer server.Close()
    41  
    42  	// When
    43  	actual, err := khttp.GetURLResponse(server.URL, false)
    44  
    45  	// Then
    46  	require.Nil(t, err)
    47  	require.Empty(t, actual)
    48  }
    49  
    50  func TestGetURLResponseSuccessTrimmed(t *testing.T) {
    51  	// Given
    52  	const expected = "     some test     "
    53  	server := httptest.NewServer(http.HandlerFunc(
    54  		func(w http.ResponseWriter, r *http.Request) {
    55  			_, err := io.WriteString(w, expected)
    56  			require.Nil(t, err)
    57  		}))
    58  	defer server.Close()
    59  
    60  	// When
    61  	actual, err := khttp.GetURLResponse(server.URL, true)
    62  
    63  	// Then
    64  	require.Nil(t, err)
    65  	require.Equal(t, strings.TrimSpace(expected), actual)
    66  }
    67  
    68  func TestGetURLResponseFailedStatus(t *testing.T) {
    69  	// Given
    70  	server := httptest.NewServer(http.HandlerFunc(
    71  		func(w http.ResponseWriter, r *http.Request) {
    72  			w.WriteHeader(http.StatusBadRequest)
    73  		}))
    74  	defer server.Close()
    75  
    76  	// When
    77  	_, err := khttp.GetURLResponse(server.URL, true)
    78  
    79  	// Then
    80  	require.NotNil(t, err)
    81  }
    82  
    83  func NewTestAgent() *khttp.Agent {
    84  	agent := khttp.NewAgent()
    85  	agent.SetImplementation(&httpfakes.FakeAgentImplementation{})
    86  	return agent
    87  }
    88  
    89  func TestAgentPost(t *testing.T) {
    90  	agent := NewTestAgent()
    91  
    92  	resp := &http.Response{
    93  		Status:        "200 OK",
    94  		StatusCode:    http.StatusOK,
    95  		Body:          io.NopCloser(bytes.NewReader([]byte("hello sig-release!"))),
    96  		ContentLength: 18,
    97  		Close:         true,
    98  		Request:       &http.Request{},
    99  	}
   100  	defer resp.Body.Close()
   101  
   102  	// First simulate a successful request
   103  	fake := &httpfakes.FakeAgentImplementation{}
   104  	fake.SendPostRequestReturns(resp, nil)
   105  
   106  	agent.SetImplementation(fake)
   107  	body, err := agent.Post("http://www.example.com/", []byte("Test string"))
   108  	require.Nil(t, err)
   109  	require.Equal(t, body, []byte("hello sig-release!"))
   110  
   111  	// Now check error is handled
   112  	fake.SendPostRequestReturns(resp, errors.New("HTTP Post error"))
   113  	agent.SetImplementation(fake)
   114  	_, err = agent.Post("http://www.example.com/", []byte("Test string"))
   115  	require.NotNil(t, err)
   116  }
   117  
   118  func TestAgentGet(t *testing.T) {
   119  	agent := NewTestAgent()
   120  
   121  	resp := &http.Response{
   122  		Status:        "200 OK",
   123  		StatusCode:    http.StatusOK,
   124  		Body:          io.NopCloser(bytes.NewReader([]byte("hello sig-release!"))),
   125  		ContentLength: 18,
   126  		Close:         true,
   127  		Request:       &http.Request{},
   128  	}
   129  	defer resp.Body.Close()
   130  
   131  	// First simulate a successful request
   132  	fake := &httpfakes.FakeAgentImplementation{}
   133  	fake.SendGetRequestReturns(resp, nil)
   134  
   135  	agent.SetImplementation(fake)
   136  	b, err := agent.Get("http://www.example.com/")
   137  	require.Nil(t, err)
   138  	require.Equal(t, b, []byte("hello sig-release!"))
   139  
   140  	// Now check error is handled
   141  	fake.SendGetRequestReturns(resp, errors.New("HTTP Post error"))
   142  	agent.SetImplementation(fake)
   143  	_, err = agent.Get("http://www.example.com/")
   144  	require.NotNil(t, err)
   145  }
   146  
   147  func TestAgentOptions(t *testing.T) {
   148  	agent := NewTestAgent()
   149  	fake := &httpfakes.FakeAgentImplementation{}
   150  	resp := &http.Response{
   151  		Status:        "Fake not found",
   152  		StatusCode:    http.StatusNotFound,
   153  		Body:          io.NopCloser(bytes.NewReader([]byte("hello sig-release!"))),
   154  		ContentLength: 18,
   155  		Close:         true,
   156  		Request:       &http.Request{},
   157  	}
   158  	defer resp.Body.Close()
   159  
   160  	fake.SendGetRequestReturns(resp, nil)
   161  	agent.SetImplementation(fake)
   162  
   163  	// Test FailOnHTTPError
   164  	// First we fail on server errors
   165  	_, err := agent.WithFailOnHTTPError(true).Get("http://example.com/")
   166  	require.NotNil(t, err)
   167  
   168  	// Then we just note them and do not fail
   169  	_, err = agent.WithFailOnHTTPError(false).Get("http://example.com/")
   170  	require.Nil(t, err)
   171  }
   172  

View as plain text