Skip to content

Commit

Permalink
Merge pull request #1892 from YaoZengzeng/stream-test
Browse files Browse the repository at this point in the history
test: unit test for cri/stream/httpstream/httpstream.go
  • Loading branch information
allencloud authored Aug 1, 2018
2 parents a8b6b19 + 6cc62d8 commit fe7fbd6
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions cri/stream/httpstream/httpstream_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

}

0 comments on commit fe7fbd6

Please sign in to comment.