vanity

vanity - A tiny server for golang vanity redirects
Log | Files | Refs | README | LICENSE

handler_test.go (3640B)


      1 /*
      2    Copyright 2021 Mark Wilkerson
      3 
      4    This program is free software: you can redistribute it and/or modify
      5     it under the terms of the GNU General Public License as published by
      6     the Free Software Foundation, either version 3 of the License.
      7 
      8     This program is distributed in the hope that it will be useful,
      9     but WITHOUT ANY WARRANTY; without even the implied warranty of
     10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11     GNU General Public License for more details.
     12 
     13     You should have received a copy of the GNU General Public License
     14     along with this program.  If not, see <https://www.gnu.org/licenses/>
     15 */
     16 package main
     17 
     18 import (
     19 	"io/ioutil"
     20 	"net/http"
     21 	"net/http/httptest"
     22 	"testing"
     23 )
     24 
     25 func TestHandler(t *testing.T) {
     26 	tt := []struct {
     27 		label      string
     28 		input      string
     29 		method     string
     30 		want       string
     31 		statuscode int
     32 		debug      bool
     33 	}{
     34 		{
     35 			label:      "Valid request",
     36 			input:      "https://whatver.biz:8080/foo?go-get=1",
     37 			method:     http.MethodGet,
     38 			want:       `<meta name="go-import" content="whatver.biz/foo git test.com/foo.git">`,
     39 			statuscode: http.StatusOK,
     40 			debug:      true,
     41 		},
     42 		{
     43 			label:      "Valid request, nested path",
     44 			input:      "https://whatver.biz:8080/foo/bar/baz?go-get=1",
     45 			method:     http.MethodGet,
     46 			want:       `<meta name="go-import" content="whatver.biz/foo/bar/baz git test.com/foo/bar/baz.git">`,
     47 			statuscode: http.StatusOK,
     48 			debug:      true,
     49 		},
     50 		{
     51 			label:      "Valid URI, bad method: POST",
     52 			input:      "https://whatver.biz:8080/foo?go-get=1",
     53 			method:     http.MethodPost,
     54 			want:       "",
     55 			statuscode: http.StatusMethodNotAllowed,
     56 		},
     57 		{
     58 			label:      "Valid URI, bad method: PUT",
     59 			input:      "https://whatver.biz:8080/foo?go-get=1",
     60 			method:     http.MethodPut,
     61 			want:       "",
     62 			statuscode: http.StatusMethodNotAllowed,
     63 		},
     64 		{
     65 			label:      "Valid URI, bad method: HEAD",
     66 			input:      "https://whatver.biz:8080/foo?go-get=1",
     67 			method:     http.MethodHead,
     68 			want:       "",
     69 			statuscode: http.StatusMethodNotAllowed,
     70 		},
     71 		{
     72 			label:      "Valid URI, bad method: DELETE",
     73 			input:      "https://whatver.biz:8080/foo?go-get=1",
     74 			method:     http.MethodDelete,
     75 			want:       "",
     76 			statuscode: http.StatusMethodNotAllowed,
     77 		},
     78 		{
     79 			label:      "Wrong go-get value",
     80 			input:      "https://whatver.biz:8080/foo?go-get=",
     81 			method:     http.MethodGet,
     82 			want:       "",
     83 			statuscode: http.StatusNotFound,
     84 		},
     85 		{
     86 			label:      "Missing go-get",
     87 			input:      "https://whatver.biz:8080/foo?go-away=1",
     88 			method:     http.MethodGet,
     89 			want:       "",
     90 			statuscode: http.StatusNotFound,
     91 		},
     92 		{
     93 			label:      "No query params",
     94 			input:      "https://whatver.biz:8080/foo",
     95 			method:     http.MethodGet,
     96 			want:       "",
     97 			statuscode: http.StatusNotFound,
     98 		},
     99 	}
    100 
    101 	for _, tc := range tt {
    102 		t.Run(tc.label, func(t *testing.T) {
    103 			opts := options{
    104 				BindAddr: "0.0.0.0",
    105 				Port:     8080,
    106 				Dest:     "test.com",
    107 				Debug:    tc.debug,
    108 			}
    109 			h := handler(opts)
    110 			req, err := http.NewRequest(tc.method, tc.input, nil)
    111 			if err != nil {
    112 				t.Fatalf("failed to create request: %v", err)
    113 			}
    114 
    115 			mock := httptest.NewRecorder()
    116 
    117 			h(mock, req)
    118 
    119 			result := mock.Result()
    120 			if result.StatusCode != tc.statuscode {
    121 				t.Fatalf("expected status %d, got %d", tc.statuscode, result.StatusCode)
    122 			}
    123 
    124 			body, err := ioutil.ReadAll(result.Body)
    125 			result.Body.Close()
    126 			if err != nil {
    127 				t.Fatalf("failed to read response body: %v", err)
    128 			}
    129 
    130 			if string(body) != tc.want {
    131 				t.Fatalf("expected %s, got %s", tc.want, body)
    132 			}
    133 
    134 		})
    135 	}
    136 }