Skip to content

Commit

Permalink
fix test error with http rpc mode
Browse files Browse the repository at this point in the history
  • Loading branch information
xiemalin committed Aug 23, 2021
1 parent db5fb3d commit 3c83f21
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions httpserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
package baidurpc_test

import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
Expand All @@ -30,7 +32,7 @@ func TestHttpRpcServerWithPath(t *testing.T) {
paramters := map[string]string{}
headers := map[string]string{}

result := sendHttpRequest(urlpath, "POST", paramters, headers)
result := sendHttpRequest(urlpath, "POST", "{\"name\":\"matt\"}", paramters, headers)
So(result, ShouldNotBeNil)
data := &baidurpc.ResponseData{}
err := json.Unmarshal(result, data)
Expand All @@ -44,7 +46,7 @@ func TestHttpRpcServerWithPath(t *testing.T) {
paramters := map[string]string{}
headers := map[string]string{}

result := sendHttpRequest(urlpath, "POST", paramters, headers)
result := sendHttpRequest(urlpath, "POST", "{\"name\":\"matt\"}", paramters, headers)
So(result, ShouldNotBeNil)
data := &baidurpc.ResponseData{}
err := json.Unmarshal(result, data)
Expand All @@ -67,7 +69,7 @@ func TestHttpRpcServer(t *testing.T) {
paramters := map[string]string{}
headers := map[string]string{}

result := sendHttpRequest(urlpath, "POST", paramters, headers)
result := sendHttpRequest(urlpath, "POST", "{\"name\":\"matt\"}", paramters, headers)

So(result, ShouldNotBeNil)
data := &baidurpc.ResponseData{}
Expand Down Expand Up @@ -98,7 +100,7 @@ func TestHttpRpcServerWithAuthenticate(t *testing.T) {
headers[baidurpc.Trace_Span_key] = "2"
headers[baidurpc.Trace_Parent_key] = "3"

result := sendHttpRequest(urlpath, "POST", paramters, headers)
result := sendHttpRequest(urlpath, "POST", "{\"name\":\"matt\"}", paramters, headers)

So(result, ShouldNotBeNil)
data := &baidurpc.ResponseData{}
Expand All @@ -109,7 +111,7 @@ func TestHttpRpcServerWithAuthenticate(t *testing.T) {
})
}

func sendHttpRequest(urlpath, method string, paramters, headers map[string]string) []byte {
func sendHttpRequest(urlpath, method, body string, paramters, headers map[string]string) []byte {
params := url.Values{}
Url, err := url.Parse(urlpath)
if err != nil {
Expand All @@ -125,12 +127,23 @@ func sendHttpRequest(urlpath, method string, paramters, headers map[string]strin
client := &http.Client{}
req, _ := http.NewRequest(method, urlPath, nil)

data := []byte(body)
bs := bytes.NewBuffer(data)
req.ContentLength = int64(len(data))
// buf := body.Bytes()
req.GetBody = func() (io.ReadCloser, error) {
r := bytes.NewReader(data)
return ioutil.NopCloser(r), nil
}

req.Body = ioutil.NopCloser(bs)

for k, v := range headers {
req.Header.Add(k, v)
}

resp, _ := client.Do(req)
body, _ := ioutil.ReadAll(resp.Body)
rbody, _ := ioutil.ReadAll(resp.Body)

return body
return rbody
}

0 comments on commit 3c83f21

Please sign in to comment.