github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/httpstream/spdy/upgrade_test.go (about) 1 /* 2 Copyright 2015 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 spdy 18 19 import ( 20 "net/http" 21 "net/http/httptest" 22 "testing" 23 ) 24 25 func TestUpgradeResponse(t *testing.T) { 26 testCases := []struct { 27 connectionHeader string 28 upgradeHeader string 29 shouldError bool 30 }{ 31 { 32 connectionHeader: "", 33 upgradeHeader: "", 34 shouldError: true, 35 }, 36 { 37 connectionHeader: "Upgrade", 38 upgradeHeader: "", 39 shouldError: true, 40 }, 41 { 42 connectionHeader: "", 43 upgradeHeader: "SPDY/3.1", 44 shouldError: true, 45 }, 46 { 47 connectionHeader: "Upgrade", 48 upgradeHeader: "SPDY/3.1", 49 shouldError: false, 50 }, 51 } 52 53 for i, testCase := range testCases { 54 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 55 upgrader := NewResponseUpgrader() 56 conn := upgrader.UpgradeResponse(w, req, nil) 57 haveErr := conn == nil 58 if e, a := testCase.shouldError, haveErr; e != a { 59 t.Fatalf("%d: expected shouldErr=%t, got %t", i, testCase.shouldError, haveErr) 60 } 61 if haveErr { 62 return 63 } 64 if conn == nil { 65 t.Fatalf("%d: unexpected nil conn", i) 66 } 67 defer conn.Close() 68 })) 69 defer server.Close() 70 71 req, err := http.NewRequest("GET", server.URL, nil) 72 if err != nil { 73 t.Fatalf("%d: error creating request: %s", i, err) 74 } 75 76 req.Header.Set("Connection", testCase.connectionHeader) 77 req.Header.Set("Upgrade", testCase.upgradeHeader) 78 79 client := &http.Client{} 80 resp, err := client.Do(req) 81 if err != nil { 82 t.Fatalf("%d: unexpected non-nil err from client.Do: %s", i, err) 83 } 84 85 if testCase.shouldError { 86 continue 87 } 88 89 if resp.StatusCode != http.StatusSwitchingProtocols { 90 t.Fatalf("%d: expected status 101 switching protocols, got %d", i, resp.StatusCode) 91 } 92 } 93 }