github.com/google/martian/v3@v3.3.3/proxyutil/proxyutil_test.go (about) 1 // Copyright 2015 Google Inc. All rights reserved. 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 proxyutil 16 17 import ( 18 "fmt" 19 "io" 20 "net/http" 21 "strings" 22 "testing" 23 ) 24 25 func TestNewResponse(t *testing.T) { 26 req, err := http.NewRequest("GET", "http://www.example.com", nil) 27 if err != nil { 28 t.Fatalf("http.NewRequest(): got %v, want no error", err) 29 } 30 req.Close = true 31 32 res := NewResponse(200, nil, req) 33 if got, want := res.StatusCode, 200; got != want { 34 t.Errorf("res.StatusCode: got %d, want %d", got, want) 35 } 36 if got, want := res.Status, "200 OK"; got != want { 37 t.Errorf("res.Status: got %q, want %q", got, want) 38 } 39 if !res.Close { 40 t.Error("res.Close: got false, want true") 41 } 42 if got, want := res.Proto, "HTTP/1.1"; got != want { 43 t.Errorf("res.Proto: got %q, want %q", got, want) 44 } 45 if got, want := res.ProtoMajor, 1; got != want { 46 t.Errorf("res.ProtoMajor: got %d, want %d", got, want) 47 } 48 if got, want := res.ProtoMinor, 1; got != want { 49 t.Errorf("res.ProtoMinor: got %d, want %d", got, want) 50 } 51 if res.Header == nil { 52 t.Error("res.Header: got nil, want header") 53 } 54 if _, ok := res.Body.(io.ReadCloser); !ok { 55 t.Error("res.Body.(io.ReadCloser): got !ok, want ok") 56 } 57 if got, want := res.Request, req; got != want { 58 t.Errorf("res.Request: got %v, want %v", got, want) 59 } 60 } 61 62 func TestWarning(t *testing.T) { 63 hdr := http.Header{} 64 err := fmt.Errorf("modifier error") 65 66 Warning(hdr, err) 67 68 if got, want := len(hdr["Warning"]), 1; got != want { 69 t.Fatalf("len(hdr[%q]): got %d, want %d", "Warning", got, want) 70 } 71 72 want := `199 "martian" "modifier error"` 73 if got := hdr["Warning"][0]; !strings.HasPrefix(got, want) { 74 t.Errorf("hdr[%q][0]: got %q, want to have prefix %q", "Warning", got, want) 75 } 76 77 hdr.Set("Date", "Mon, 02 Jan 2006 15:04:05 GMT") 78 Warning(hdr, err) 79 80 if got, want := len(hdr["Warning"]), 2; got != want { 81 t.Fatalf("len(hdr[%q]): got %d, want %d", "Warning", got, want) 82 } 83 84 want = `199 "martian" "modifier error" "Mon, 02 Jan 2006 15:04:05 GMT"` 85 if got := hdr["Warning"][1]; got != want { 86 t.Errorf("hdr[%q][1]: got %q, want %q", "Warning", got, want) 87 } 88 }