From 6cc62d8d042032d1a1d7463801ba0e1778cf98d5 Mon Sep 17 00:00:00 2001 From: YaoZengzeng Date: Tue, 24 Jul 2018 19:56:17 +0800 Subject: [PATCH] test: unit test for cri/stream/httpstream/httpstream.go Signed-off-by: YaoZengzeng --- cri/stream/httpstream/httpstream_test.go | 100 +++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 cri/stream/httpstream/httpstream_test.go diff --git a/cri/stream/httpstream/httpstream_test.go b/cri/stream/httpstream/httpstream_test.go new file mode 100644 index 000000000..35e01db39 --- /dev/null +++ b/cri/stream/httpstream/httpstream_test.go @@ -0,0 +1,100 @@ +package httpstream + +import ( + "net/http" + "net/http/httptest" + "reflect" + "testing" + + "github.com/alibaba/pouch/cri/stream/constant" +) + +func TestIsUpgradeRequest(t *testing.T) { + req, err := http.NewRequest("", "", nil) + if err != nil { + t.Fatalf("create new http request failed: %v", err) + } + + if IsUpgradeRequest(req) { + t.Fatalf("IsUpgradeRequest return true, but the request dose not include upgrade header") + } + + req.Header.Set(http.CanonicalHeaderKey(HeaderConnection), HeaderUpgrade) + + if !IsUpgradeRequest(req) { + t.Fatalf("IsUpgradeRequest return false, but the request dose include upgrade header") + } +} + +func TestNegotiateProtocol(t *testing.T) { + clientProtocols := []string{constant.StreamProtocolV1Name, constant.StreamProtocolV2Name} + serverProtocls := []string{constant.StreamProtocolV2Name, constant.StreamProtocolV3Name} + + protocol := negotiateProtocol(clientProtocols, serverProtocls) + if protocol != constant.StreamProtocolV2Name { + t.Fatalf("negotiateProtocol could not find the common protocol") + } + + clientProtocols = []string{} + protocol = negotiateProtocol(clientProtocols, serverProtocls) + if protocol != "" { + t.Fatalf("negotiateProtocol should return empty string when there is no common protocol") + } +} + +func TestHandshake(t *testing.T) { + req, err := http.NewRequest("", "", nil) + if err != nil { + t.Fatalf("create new http request failed: %v", err) + } + + w := httptest.NewRecorder() + + serverProtocls := []string{} + + // clientProtocols and serverProtocls both are empty. + protocol, err := Handshake(w, req, serverProtocls) + if protocol != "" { + t.Fatalf("clientProtocols and serverProtocls both are empty, returned protocol should be empty") + } + + // clientProtocols is empty. + serverProtocls = []string{constant.StreamProtocolV3Name} + protocol, err = Handshake(w, req, serverProtocls) + if protocol != "" { + t.Fatalf("clientProtocols is empty, returned protocol should be empty") + } + + // serverProtocls is empty. + req.Header.Add(http.CanonicalHeaderKey(HeaderProtocolVersion), constant.StreamProtocolV1Name) + protocol, err = Handshake(w, req, []string{}) + if protocol != "" { + t.Fatalf("serverProtocls is empty, returned protocol should be empty") + } + + // clientProtocols and serverProtocls have no common protocol. + protocol, err = Handshake(w, req, serverProtocls) + if protocol != "" { + t.Fatalf("no common protocol, returned protocol should be empty") + } + if w.Code != http.StatusForbidden { + t.Fatalf("no common protocol, the status of response should be StatusForbidden") + } + if !reflect.DeepEqual(w.HeaderMap[http.CanonicalHeaderKey(HeaderAcceptedProtocolVersions)], serverProtocls) { + t.Fatalf("no common protocol, the response should include the protocols which server support") + } + + // successful handshake. + w = httptest.NewRecorder() + serverProtocls = []string{constant.StreamProtocolV2Name, constant.StreamProtocolV3Name} + req.Header.Add(http.CanonicalHeaderKey(HeaderProtocolVersion), constant.StreamProtocolV2Name) + + protocol, err = Handshake(w, req, serverProtocls) + if protocol != constant.StreamProtocolV2Name { + t.Fatalf("Handshake should return the common protocol") + } + if w.Header().Get(HeaderProtocolVersion) != constant.StreamProtocolV2Name { + t.Fatalf("Handshake should write the negotiated protocol into the header of response") + } + +}