vitess.io/vitess@v0.16.2/go/vt/throttler/throttlerz_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess 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 throttler
    18  
    19  import (
    20  	"net/http"
    21  	"net/http/httptest"
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  func TestThrottlerzHandler_MissingSlash(t *testing.T) {
    27  	request, _ := http.NewRequest("GET", "/throttlerz", nil)
    28  	response := httptest.NewRecorder()
    29  	m := newManager()
    30  
    31  	throttlerzHandler(response, request, m)
    32  
    33  	if got, want := response.Body.String(), "invalid /throttlerz path"; !strings.Contains(got, want) {
    34  		t.Fatalf("/throttlerz without the slash does not work (the Go HTTP server does automatically redirect in practice though). got = %v, want = %v", got, want)
    35  	}
    36  }
    37  
    38  func TestThrottlerzHandler_List(t *testing.T) {
    39  	f := &managerTestFixture{}
    40  	if err := f.setUp(); err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	defer f.tearDown()
    44  
    45  	request, _ := http.NewRequest("GET", "/throttlerz/", nil)
    46  	response := httptest.NewRecorder()
    47  
    48  	throttlerzHandler(response, request, f.m)
    49  
    50  	if got, want := response.Body.String(), `<a href="/throttlerz/t1">t1</a>`; !strings.Contains(got, want) {
    51  		t.Fatalf("list does not include 't1'. got = %v, want = %v", got, want)
    52  	}
    53  	if got, want := response.Body.String(), `<a href="/throttlerz/t2">t2</a>`; !strings.Contains(got, want) {
    54  		t.Fatalf("list does not include 't1'. got = %v, want = %v", got, want)
    55  	}
    56  }
    57  
    58  func TestThrottlerzHandler_Details(t *testing.T) {
    59  	f := &managerTestFixture{}
    60  	if err := f.setUp(); err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	defer f.tearDown()
    64  
    65  	request, _ := http.NewRequest("GET", "/throttlerz/t1", nil)
    66  	response := httptest.NewRecorder()
    67  
    68  	throttlerzHandler(response, request, f.m)
    69  
    70  	if got, want := response.Body.String(), `<title>Details for Throttler 't1'</title>`; !strings.Contains(got, want) {
    71  		t.Fatalf("details for 't1' not shown. got = %v, want = %v", got, want)
    72  	}
    73  }