Skip to content

Commit

Permalink
update go.mod, remove external dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
lapitskyss committed Nov 22, 2021
1 parent 49a594e commit a06c5a6
Show file tree
Hide file tree
Showing 7 changed files with 448 additions and 44 deletions.
7 changes: 1 addition & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
module github.com/lapitskyss/jsonrpc

go 1.16

require (
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.11.0
)
go 1.17
17 changes: 0 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +0,0 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.11.0 h1:C16pk7tQNiH6VlCrtIXL1w8GaOsi1X3W8KDkE1BuYd4=
github.com/tidwall/gjson v1.11.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 1 addition & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sync"

"github.com/lapitskyss/jsonrpc/jparser"
"github.com/tidwall/gjson"
)

// ServeHTTP process incoming requests.
Expand All @@ -34,7 +33,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

if !gjson.ValidBytes(json) {
if err = jparser.ValidateBytes(json); err != nil {
sendParseError(w)
return
}
Expand Down
64 changes: 51 additions & 13 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package jsonrpc

import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"

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

func TestServeHTTP(t *testing.T) {
Expand Down Expand Up @@ -63,22 +63,24 @@ func TestServeHTTP(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
res, err := http.Post(ts.URL, "application/json", bytes.NewBufferString(c.in))
if err != nil {
require.NoError(t, err)
t.Errorf("Received unexpected error:\n%+v", err)
t.FailNow()
}

resp, err := ioutil.ReadAll(res.Body)
if err != nil {
require.NoError(t, err)
t.Errorf("Received unexpected error:\n%+v", err)
t.FailNow()
}
err = res.Body.Close()
if err != nil {
require.NoError(t, err)
t.Errorf("Received unexpected error:\n%+v", err)
t.FailNow()
}

if c.out == "" {
require.Equal(t, c.out, string(resp))
} else {
require.JSONEq(t, c.out, string(resp))
if !IsJSONEqual(c.out, string(resp)) {
t.Errorf("Unexpected result. Expected %v. Got %v", c.out, string(resp))
t.FailNow()
}
})
}
Expand Down Expand Up @@ -127,9 +129,15 @@ func Test_handleRequest(t *testing.T) {
rpc.Register("sum", sumService.sum)

r, _ := http.NewRequest("POST", "/", nil)
json := []byte(`{"jsonrpc": "2.0", "method": "sum", "params": [1, 2, 3, 4], "id": "1" }`)
j := []byte(`{"jsonrpc": "2.0", "method": "sum", "params": [1, 2, 3, 4], "id": "1" }`)

res := rpc.handleRequest(r, j)
expected := `{"jsonrpc":"2.0","result":10,"id":"1"}`

rpc.handleRequest(r, json)
if !IsJSONEqual(expected, string(res)) {
t.Errorf("Unexpected result. Expected %v. Got %v", expected, string(res))
t.FailNow()
}
}

func Benchmark_handleRequest(b *testing.B) {
Expand All @@ -138,14 +146,44 @@ func Benchmark_handleRequest(b *testing.B) {
rpc.Register("sum", sumService.sum)

r, _ := http.NewRequest("POST", "/", nil)
json := []byte(`{"jsonrpc": "2.0", "method": "sum", "params": [1, 2, 3, 4], "id": "1" }`)
j := []byte(`{"jsonrpc": "2.0", "method": "sum", "params": [1, 2, 3, 4], "id": "1" }`)

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
rpc.handleRequest(r, json)
rpc.handleRequest(r, j)
}
}

func IsJSONEqual(expected string, actual string) bool {
if expected == "" {
return expected == actual
}

var e, a interface{}

err := json.Unmarshal([]byte(expected), &e)
if err != nil {
return false
}

err = json.Unmarshal([]byte(actual), &a)
if err != nil {
return false
}

exp, ok := e.([]byte)
if !ok {
return reflect.DeepEqual(e, a)
}

act, ok := a.([]byte)
if !ok {
return false
}

return string(exp) == string(act)
}

type SumService struct {
Expand Down
1 change: 1 addition & 0 deletions jparser/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MIT License

Copyright (c) 2016 Leonid Bugaev
Copyright (c) 2018 Aliaksandr Valialkin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
24 changes: 18 additions & 6 deletions jparser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jparser

import (
"github.com/stretchr/testify/require"
"reflect"
"testing"
)

Expand All @@ -10,7 +10,10 @@ var arr = []byte(`[{"jsonrpc":"2.0","method":"sum","params":[1],"id":1}, {"jsonr
func TestArrayLength(t *testing.T) {
l := ArrayLength(arr)

require.Equal(t, 3, l)
if !reflect.DeepEqual(3, l) {
t.Errorf("Unexpected result. Expected %v. Got %v", 3, l)
t.FailNow()
}
}

func BenchmarkArrayLength(b *testing.B) {
Expand All @@ -24,9 +27,12 @@ func BenchmarkArrayLength(b *testing.B) {

func TestArrayElement(t *testing.T) {
element := ArrayElement(arr, 1)
expected := []byte(`{"jsonrpc":"2.0","method":"sum","params":[1, 2],"id":2}`)
expected := `{"jsonrpc":"2.0","method":"sum","params":[1, 2],"id":2}`

require.Equal(t, expected, element)
if expected != string(element) {
t.Errorf("Unexpected result. Expected %v. Got %v", expected, string(element))
t.FailNow()
}
}

func BenchmarkArrayElement(b *testing.B) {
Expand All @@ -40,8 +46,14 @@ func BenchmarkArrayElement(b *testing.B) {

func TestIsArray(t *testing.T) {
isArr := IsArray([]byte(` [1, 2]`))
require.Equal(t, true, isArr)
if !reflect.DeepEqual(true, isArr) {
t.Errorf("Unexpected result. Expected %v. Got %v", true, isArr)
t.FailNow()
}

isArr2 := IsArray([]byte(`{"jsonrpc":"2.0","method":"sum","params":[1, 2],"id":2}`))
require.Equal(t, false, isArr2)
if !reflect.DeepEqual(false, isArr2) {
t.Errorf("Unexpected result. Expected %v. Got %v", false, isArr2)
t.FailNow()
}
}
Loading

0 comments on commit a06c5a6

Please sign in to comment.