9 "www.bamsoftware.com/git/champa.git/turbotunnel"
12 func mustNewRequest(method
, path
string) *http
.Request
{
13 req
, err
:= http
.NewRequest(method
, path
, nil)
20 func TestDecodeRequest(t
*testing
.T
) {
21 defClientID
:= turbotunnel
.ClientID
{'C', 'L', 'I', 'E', 'N', 'T', 'I', 'D'}
22 defPayload
:= []byte("abcde")
23 defEncoded
:= string(base64
.RawURLEncoding
.EncodeToString(bytes
.Join([][]byte{defClientID
[:], defPayload
}, nil)))
25 for _
, test
:= range []struct {
27 expectedClientID turbotunnel
.ClientID
28 expectedPayload
[]byte
31 {"/0/XXXX/" + defEncoded
, defClientID
, defPayload
},
32 {"/0/@@@@/" + defEncoded
, defClientID
, defPayload
}, // non-base64 padding
33 {"/0/XXXX/YYYY/" + defEncoded
, defClientID
, defPayload
},
34 {"/0" + defEncoded
, defClientID
, defPayload
}, // no slash necessary after version
37 {"/0", turbotunnel
.ClientID
{}, nil},
38 {"//0/XXXX/" + defEncoded
, turbotunnel
.ClientID
{}, nil}, // extra slash at start
39 {"0/XXXX/" + defEncoded
, turbotunnel
.ClientID
{}, nil}, // no slash at start
40 {"/0/@@@@" + defEncoded
, turbotunnel
.ClientID
{}, nil}, // non-base64 in payload
41 {"/0/XXXX/" + defEncoded
+ "/", turbotunnel
.ClientID
{}, nil},
42 {"/1/XXXX/" + defEncoded
, turbotunnel
.ClientID
{}, nil}, // unknown version
43 {"/0/XXXX/Q0xJRU5USURhYmNkZQ===", turbotunnel
.ClientID
{}, nil}, // with padding characters
45 req
:= mustNewRequest("GET", test
.path
)
46 clientID
, payload
:= decodeRequest(req
)
47 if !bytes
.Equal(payload
, test
.expectedPayload
) || clientID
!= test
.expectedClientID
{
48 t
.Errorf("%+q → (%v, %v), expected (%v, %v)",
49 test
.path
, clientID
, payload
, test
.expectedClientID
, test
.expectedPayload
)