go.etcd.io/etcd@v3.3.27+incompatible/proxy/httpproxy/proxy_test.go (about)

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package httpproxy
    16  
    17  import (
    18  	"io/ioutil"
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"net/url"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  func TestReadonlyHandler(t *testing.T) {
    27  	fixture := func(w http.ResponseWriter, req *http.Request) {
    28  		w.WriteHeader(http.StatusOK)
    29  	}
    30  	hdlrFunc := readonlyHandlerFunc(http.HandlerFunc(fixture))
    31  
    32  	tests := []struct {
    33  		method string
    34  		want   int
    35  	}{
    36  		// GET is only passing method
    37  		{"GET", http.StatusOK},
    38  
    39  		// everything but GET is StatusNotImplemented
    40  		{"POST", http.StatusNotImplemented},
    41  		{"PUT", http.StatusNotImplemented},
    42  		{"PATCH", http.StatusNotImplemented},
    43  		{"DELETE", http.StatusNotImplemented},
    44  		{"FOO", http.StatusNotImplemented},
    45  	}
    46  
    47  	for i, tt := range tests {
    48  		req, _ := http.NewRequest(tt.method, "http://example.com", nil)
    49  		rr := httptest.NewRecorder()
    50  		hdlrFunc(rr, req)
    51  
    52  		if tt.want != rr.Code {
    53  			t.Errorf("#%d: incorrect HTTP status code: method=%s want=%d got=%d", i, tt.method, tt.want, rr.Code)
    54  		}
    55  	}
    56  }
    57  
    58  func TestConfigHandlerGET(t *testing.T) {
    59  	var err error
    60  	us := make([]*url.URL, 3)
    61  	us[0], err = url.Parse("http://example1.com")
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	us[1], err = url.Parse("http://example2.com")
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	us[2], err = url.Parse("http://example3.com")
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	rp := reverseProxy{
    75  		director: &director{
    76  			ep: []*endpoint{
    77  				newEndpoint(*us[0], 1*time.Second),
    78  				newEndpoint(*us[1], 1*time.Second),
    79  				newEndpoint(*us[2], 1*time.Second),
    80  			},
    81  		},
    82  	}
    83  
    84  	req, _ := http.NewRequest("GET", "http://example.com//v2/config/local/proxy", nil)
    85  	rr := httptest.NewRecorder()
    86  	rp.configHandler(rr, req)
    87  
    88  	wbody := "{\"endpoints\":[\"http://example1.com\",\"http://example2.com\",\"http://example3.com\"]}\n"
    89  
    90  	body, err := ioutil.ReadAll(rr.Body)
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	if string(body) != wbody {
    96  		t.Errorf("body = %s, want %s", string(body), wbody)
    97  	}
    98  }