Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
suchen-sci committed Mar 25, 2022
1 parent 10361dd commit c823a66
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
111 changes: 111 additions & 0 deletions pkg/protocols/httpprot/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package httpprot

import (
"io"
"net/http"
"strings"
"sync"
"testing"

"github.com/stretchr/testify/assert"
)

func TestHeader(t *testing.T) {
assert := assert.New(t)
req, err := http.NewRequest(http.MethodGet, "http:https://127.0.0.1:8080", strings.NewReader("body string"))
assert.Nil(err)

multiEqual := func(want interface{}, got []interface{}) {
for _, g := range got {
assert.Equal(want, g)
}
}

header := newHeader(req.Header)
header.Add("X-Users", "abc123")
header.Add("X-Users", "def123")
multiEqual([]string{"abc123", "def123"}, []interface{}{header.Values("X-Users"), req.Header.Values("X-Users")})
multiEqual("abc123", []interface{}{header.Get("X-Users"), req.Header.Get("X-Users")})

header.Set("X-Users", "qwe123")
multiEqual([]string{"qwe123"}, []interface{}{header.Values("X-Users"), req.Header.Values("X-Users")})
multiEqual("qwe123", []interface{}{header.Get("X-Users"), req.Header.Get("X-Users")})

header.Del("X-Users")
multiEqual([]string(nil), []interface{}{header.Values("X-Users"), req.Header.Values("X-Users")})
multiEqual("", []interface{}{header.Get("X-Users"), req.Header.Get("X-Users")})

header2 := header.Clone()
header2.Add("X-Users", "header2")
multiEqual([]string(nil), []interface{}{header.Values("X-Users"), req.Header.Values("X-Users")})
multiEqual("", []interface{}{header.Get("X-Users"), req.Header.Get("X-Users")})
assert.Equal([]string{"header2"}, header2.Values("X-Users"))
assert.Equal("header2", header2.Get("X-Users"))

header.Add("X-User", "abc")
header.Add("X-Device", "phone")
res := map[string][]string{}
header.Iter(func(key string, values []string) {
res[key] = values
})
assert.Equal(map[string][]string{"X-User": {"abc"}, "X-Device": {"phone"}}, res)
}

func TestPayload(t *testing.T) {
assert := assert.New(t)

reader := strings.NewReader("body string")
payload := newPayload(reader)

wg := sync.WaitGroup{}
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
defer wg.Done()
newReader := payload.NewReader()
data, err := io.ReadAll(newReader)
assert.Nil(err)
assert.Equal("body string", string(data))
}()
}
wg.Wait()

payload.SetReader(strings.NewReader("new body"), true)
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
defer wg.Done()
newReader := payload.NewReader()
data, err := io.ReadAll(newReader)
assert.Nil(err)
assert.Equal("new body", string(data))
}()
}
wg.Wait()

// close only close original reader, the buffer not changed.
// so in current implementation, after close payload, we still
// can read data from reader
newReader := payload.NewReader()
payload.Close()
data, err := io.ReadAll(newReader)
assert.Nil(err)
assert.Equal("new body", string(data))
}
83 changes: 83 additions & 0 deletions pkg/protocols/httpprot/request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package httpprot

import (
"context"
"io"
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRequest(t *testing.T) {
assert := assert.New(t)
req, err := http.NewRequest(http.MethodGet, "http:https://127.0.0.1:80", strings.NewReader("body string"))
assert.Nil(err)

request := NewRequest(req)
assert.Equal(req, request.Std())
assert.Equal("", request.RealIP())
assert.Equal("HTTP/1.1", request.Proto())
assert.Equal(req.URL, request.URL())
assert.Equal(http.MethodGet, request.Method())

// when SetMethod, both Request and http.Request method should be changed
request.SetMethod(http.MethodDelete)
assert.Equal(http.MethodDelete, request.Method())
assert.Equal(http.MethodDelete, req.Method)

// Request and http.Request should share same cookie
assert.Equal([]*http.Cookie{}, request.Cookies())
assert.Equal([]*http.Cookie{}, req.Cookies())

request.AddCookie(&http.Cookie{
Name: "cookie",
Value: "123",
})
cookie, err := request.Cookie("cookie")
assert.Nil(err)
assert.Equal("123", cookie.Value)
cookie, err = req.Cookie("cookie")
assert.Nil(err)
assert.Equal("123", cookie.Value)

// Request and http.Request should share same header
assert.Equal(req.Header.Get("Cookie"), request.Header().Get("Cookie"))

// Payload reader and http.Request reader should both work
reader := request.Payload().NewReader()
data, err := io.ReadAll(reader)
assert.Nil(err)
assert.Equal("body string", string(data))

data, err = io.ReadAll(req.Body)
assert.Nil(err)
assert.Equal("body string", string(data))

// check context
ctx, cancel := context.WithCancel(request.Context())
request.WithContext(ctx)
assert.Nil(request.Context().Err())

cancel()
assert.Equal(context.Canceled, request.Context().Err())
assert.Equal(context.Canceled, request.Std().Context().Err())
}
Empty file.

0 comments on commit c823a66

Please sign in to comment.