Skip to content

Commit

Permalink
add test for just fixed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
powerman committed Oct 13, 2016
1 parent 81335e6 commit dd19550
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions jsonrpc2/context_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package jsonrpc2_test

import (
"bytes"
"context"
"encoding/json"
"net"
"net/http"
"net/rpc"
Expand Down Expand Up @@ -183,3 +185,57 @@ func TestContext(t *testing.T) {
}
}
}

func TestContextBugNoContextWithoutParams(t *testing.T) {
cases := []struct {
req string
want interface{}
}{
{
`{"jsonrpc":"2.0","id":0,"method":"CtxSvc.NameCtx"}`,
map[string]interface{}{
"Name": " ",
"TCPRemoteAddr": "127.0.0.1",
"HTTPRemoteAddr": "",
},
},
{
`{"jsonrpc":"2.0","id":0,"method":"CtxSvc.NameCtx","params":{}}`,
map[string]interface{}{
"Name": " ",
"TCPRemoteAddr": "127.0.0.1",
"HTTPRemoteAddr": "",
},
},
{
`{"jsonrpc":"2.0","id":0,"method":"CtxSvc.NameCtx","params":{"Fname":"First","Lname":"Last"}}`,
map[string]interface{}{
"Name": "First Last",
"TCPRemoteAddr": "127.0.0.1",
"HTTPRemoteAddr": "",
},
},
}
for _, v := range cases {
buf := bytes.NewBufferString(v.req)
rpc.ServeRequest(jsonrpc2.NewServerCodecContext(
context.WithValue(context.Background(), remoteAddrContextKey, &net.TCPAddr{IP: net.ParseIP("127.0.0.1")}),
&bufReadWriteCloser{buf},
nil,
))
var res map[string]interface{}
err := json.Unmarshal(buf.Bytes(), &res)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(v.want, res["result"]) {
t.Errorf("%q:\n\n\texp: %#v\n\n\tgot: %#v\n\n", v.req, v.want, res["result"])
}
}
}

type bufReadWriteCloser struct {
*bytes.Buffer
}

func (b *bufReadWriteCloser) Close() error { return nil }

0 comments on commit dd19550

Please sign in to comment.