k8s.io/client-go@v0.31.1/util/testing/fake_handler_test.go (about)

     1  /*
     2  Copyright 2014 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 testing
    18  
    19  import (
    20  	"bytes"
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"testing"
    24  )
    25  
    26  func TestFakeHandlerPath(t *testing.T) {
    27  	handler := FakeHandler{StatusCode: http.StatusOK}
    28  	server := httptest.NewServer(&handler)
    29  	defer server.Close()
    30  	method := "GET"
    31  	path := "/foo/bar"
    32  	body := "somebody"
    33  
    34  	req, err := http.NewRequest(method, server.URL+path, bytes.NewBufferString(body))
    35  	if err != nil {
    36  		t.Errorf("unexpected error: %v", err)
    37  	}
    38  
    39  	client := http.Client{}
    40  	_, err = client.Do(req)
    41  	if err != nil {
    42  		t.Errorf("unexpected error: %v", err)
    43  	}
    44  
    45  	handler.ValidateRequest(t, path, method, &body)
    46  }
    47  
    48  func TestFakeHandlerPathNoBody(t *testing.T) {
    49  	handler := FakeHandler{StatusCode: http.StatusOK}
    50  	server := httptest.NewServer(&handler)
    51  	defer server.Close()
    52  	method := "GET"
    53  	path := "/foo/bar"
    54  
    55  	req, err := http.NewRequest(method, server.URL+path, nil)
    56  	if err != nil {
    57  		t.Errorf("unexpected error: %v", err)
    58  	}
    59  
    60  	client := http.Client{}
    61  	_, err = client.Do(req)
    62  	if err != nil {
    63  		t.Errorf("unexpected error: %v", err)
    64  	}
    65  
    66  	handler.ValidateRequest(t, path, method, nil)
    67  }
    68  
    69  type fakeError struct {
    70  	errors []string
    71  }
    72  
    73  func (f *fakeError) Errorf(format string, args ...interface{}) {
    74  	f.errors = append(f.errors, format)
    75  }
    76  
    77  func (f *fakeError) Logf(format string, args ...interface{}) {}
    78  
    79  func TestFakeHandlerWrongPath(t *testing.T) {
    80  	handler := FakeHandler{StatusCode: http.StatusOK}
    81  	server := httptest.NewServer(&handler)
    82  	defer server.Close()
    83  	method := "GET"
    84  	path := "/foo/bar"
    85  	fakeT := fakeError{}
    86  
    87  	req, err := http.NewRequest(method, server.URL+"/foo/baz", nil)
    88  	if err != nil {
    89  		t.Errorf("unexpected error: %v", err)
    90  	}
    91  
    92  	client := http.Client{}
    93  	_, err = client.Do(req)
    94  	if err != nil {
    95  		t.Errorf("unexpected error: %v", err)
    96  	}
    97  
    98  	handler.ValidateRequest(&fakeT, path, method, nil)
    99  	if len(fakeT.errors) != 1 {
   100  		t.Errorf("Unexpected error set: %#v", fakeT.errors)
   101  	}
   102  }
   103  
   104  func TestFakeHandlerWrongMethod(t *testing.T) {
   105  	handler := FakeHandler{StatusCode: http.StatusOK}
   106  	server := httptest.NewServer(&handler)
   107  	defer server.Close()
   108  	method := "GET"
   109  	path := "/foo/bar"
   110  	fakeT := fakeError{}
   111  
   112  	req, err := http.NewRequest("PUT", server.URL+path, nil)
   113  	if err != nil {
   114  		t.Errorf("unexpected error: %v", err)
   115  	}
   116  
   117  	client := http.Client{}
   118  	_, err = client.Do(req)
   119  	if err != nil {
   120  		t.Errorf("unexpected error: %v", err)
   121  	}
   122  
   123  	handler.ValidateRequest(&fakeT, path, method, nil)
   124  	if len(fakeT.errors) != 1 {
   125  		t.Errorf("Unexpected error set: %#v", fakeT.errors)
   126  	}
   127  }
   128  
   129  func TestFakeHandlerWrongBody(t *testing.T) {
   130  	handler := FakeHandler{StatusCode: http.StatusOK}
   131  	server := httptest.NewServer(&handler)
   132  	defer server.Close()
   133  	method := "GET"
   134  	path := "/foo/bar"
   135  	body := "somebody"
   136  	fakeT := fakeError{}
   137  
   138  	req, err := http.NewRequest(method, server.URL+path, bytes.NewBufferString(body))
   139  	if err != nil {
   140  		t.Errorf("unexpected error: %v", err)
   141  	}
   142  
   143  	client := http.Client{}
   144  	_, err = client.Do(req)
   145  	if err != nil {
   146  		t.Errorf("unexpected error: %v", err)
   147  	}
   148  
   149  	otherbody := "otherbody"
   150  	handler.ValidateRequest(&fakeT, path, method, &otherbody)
   151  	if len(fakeT.errors) != 1 {
   152  		t.Errorf("Unexpected error set: %#v", fakeT.errors)
   153  	}
   154  }
   155  
   156  func TestFakeHandlerNilBody(t *testing.T) {
   157  	handler := FakeHandler{StatusCode: http.StatusOK}
   158  	server := httptest.NewServer(&handler)
   159  	defer server.Close()
   160  	method := "GET"
   161  	path := "/foo/bar"
   162  	body := "somebody"
   163  	fakeT := fakeError{}
   164  
   165  	req, err := http.NewRequest(method, server.URL+path, nil)
   166  	if err != nil {
   167  		t.Errorf("unexpected error: %v", err)
   168  	}
   169  
   170  	client := http.Client{}
   171  	_, err = client.Do(req)
   172  	if err != nil {
   173  		t.Errorf("unexpected error: %v", err)
   174  	}
   175  
   176  	handler.ValidateRequest(&fakeT, path, method, &body)
   177  	if len(fakeT.errors) != 1 {
   178  		t.Errorf("Unexpected error set: %#v", fakeT.errors)
   179  	}
   180  }