Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pingcap check #9

Merged
merged 3 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions adatper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
// 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.

Expand Down
1 change: 1 addition & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
// 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.

Expand Down
28 changes: 9 additions & 19 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
// 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.

Expand All @@ -18,12 +19,10 @@ import (
"net/http"
"testing"

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

var (
errTest = errors.New("test")
)
var errTest = errors.New("test")

type withError struct {
err error
Expand All @@ -37,27 +36,18 @@ func (w *withError) Unwrap() error {
return w.err
}

type errSuite struct{}

func TestError(t *testing.T) {
TestingT(t)
}

var _ = Suite(&errSuite{})

// TestWithError test UnwrapErrorStatusCode method
func (e *errSuite) TestWithError(c *C) {
func TestWithError(t *testing.T) {
var err error = &withError{
err: ErrorWithStatusCode(errTest, http.StatusInternalServerError),
}
code, ok := UnwrapErrorStatusCode(err)
c.Assert(ok, IsTrue)
c.Assert(code == http.StatusInternalServerError, IsTrue)
require.True(t, ok)
require.Equal(t, http.StatusInternalServerError, code)
}

// TestOriginError ErrorWithStatusCode Unwrap
func (e *errSuite) TestOriginError(c *C) {
// TestOriginError test ErrorWithStatusCode Unwrap
func TestOriginError(t *testing.T) {
err := ErrorWithStatusCode(errTest, http.StatusInternalServerError)
err = Unwrap(err)
c.Assert(err == errTest, IsTrue)
require.Equal(t, errTest, Unwrap(err))
}
1 change: 1 addition & 0 deletions form_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
// 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.

Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module github.com/pingcap/fn

go 1.11

require github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/stretchr/testify v1.7.1
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 // indirect
)
15 changes: 13 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8 h1:USx2/E1bX46VG32FIw034Au6seQ2fY9NEILmNh/UlQg=
github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8/go.mod h1:B1+S9LNcuMyLH/4HMTViQOJevkGiik3wW2AN9zb2fNQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/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.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 h1:dbuHpmKjkDzSOMKAWl10QNlgaZUd3V1q99xc81tt2Kc=
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 3 additions & 1 deletion go113.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build go1.13
//go:build go1.13
// +build go1.13

// Copyright 2020 PingCAP, Inc.
//
Expand All @@ -10,6 +11,7 @@
//
// 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.

Expand Down
1 change: 1 addition & 0 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
// 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.

Expand Down
1 change: 1 addition & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
// 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.

Expand Down
1 change: 1 addition & 0 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
// 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.

Expand Down
4 changes: 3 additions & 1 deletion pre_go113.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build !go1.13
//go:build !go1.13
// +build !go1.13

// Copyright 2020 PingCAP, Inc.
//
Expand All @@ -10,6 +11,7 @@
//
// 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.

Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
// 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.

Expand Down
1 change: 1 addition & 0 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
// 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.

Expand Down
1 change: 1 addition & 0 deletions wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
// 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.

Expand Down
73 changes: 31 additions & 42 deletions wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
// 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.

Expand All @@ -24,20 +25,11 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"

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

type fnSuite struct{}

func TestFn(t *testing.T) {
TestingT(t)
}

var _ = Suite(&fnSuite{})

type testRequest struct {
Foo string `json:"foo"`
Bar int `json:"bar"`
Expand Down Expand Up @@ -90,7 +82,7 @@ func withAll(io.ReadCloser, *testRequest, Form, PostForm, http.Header, *multipar
return nil, nil
}

func (s *fnSuite) TestHandler(c *C) {
func TestHandler(t *testing.T) {
Wrap(withNone)
Wrap(withBody)
Wrap(withReq)
Expand All @@ -108,55 +100,55 @@ func (s *fnSuite) TestHandler(c *C) {
Wrap(withInContextAndPayload)
}

func (s *fnSuite) TestPlugin(c *C) {
func TestPlugin(t *testing.T) {
logic := func(ctx context.Context) (*testResponse, error) {
c.Assert(ctx.Value("key").(string) == "value", IsTrue)
c.Assert(ctx.Value("key2").(string) == "value2", IsTrue)
require.Equal(t, "value", ctx.Value("key").(string))
require.Equal(t, "value2", ctx.Value("key2").(string))
return &testResponse{}, nil
}

plugin1 := func(ctx context.Context, request *http.Request) (context.Context, error) {
c.Assert(ctx.Value("global1").(string) == "globalvalue1", IsTrue)
c.Assert(ctx.Value("global2").(string) == "globalvalue2", IsTrue)
require.Equal(t, "globalvalue1", ctx.Value("global1").(string))
require.Equal(t, "globalvalue2", ctx.Value("global2").(string))
return context.WithValue(ctx, "key", "value"), nil
}

plugin2 := func(ctx context.Context, request *http.Request) (context.Context, error) {
c.Assert(ctx.Value("global1").(string) == "globalvalue1", IsTrue)
c.Assert(ctx.Value("global2").(string) == "globalvalue2", IsTrue)
c.Assert(ctx.Value("key").(string) == "value", IsTrue)
require.Equal(t, "globalvalue1", ctx.Value("global1").(string))
require.Equal(t, "globalvalue2", ctx.Value("global2").(string))
require.Equal(t, "value", ctx.Value("key").(string))
return context.WithValue(ctx, "key2", "value2"), nil
}

handler := Wrap(logic).Plugin(plugin1, plugin2)

recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
c.Assert(err, IsNil)
require.NoError(t, err)
handler.ServeHTTP(recorder, request)
}

func (s *fnSuite) TestGroupPlugin(c *C) {
func TestGroupPlugin(t *testing.T) {
group := NewGroup()
group.Plugin(func(ctx context.Context, request *http.Request) (context.Context, error) {
return context.WithValue(ctx, "key", "value"), nil
})

logic := func(ctx context.Context) (*testResponse, error) {
c.Assert(ctx.Value("key").(string) == "value", IsTrue)
require.Equal(t, "value", ctx.Value("key").(string))
return &testResponse{}, nil
}
handler := group.Wrap(logic)

recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
c.Assert(err, IsNil)
require.NoError(t, err)
handler.ServeHTTP(recorder, request)
}

func (s *fnSuite) TestSetResponseEncoder(c *C) {
func TestSetResponseEncoder(t *testing.T) {
handler := Wrap(func(ctx context.Context, request *http.Request) (context.Context, error) {
return nil, nil
return context.TODO(), nil
})

testResp := &testResponse{
Expand All @@ -169,14 +161,14 @@ func (s *fnSuite) TestSetResponseEncoder(c *C) {

recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
c.Assert(err, IsNil)
require.NoError(t, err)
handler.ServeHTTP(recorder, request)
respMsg := &testResponse{}
_ = json.Unmarshal(recorder.Body.Bytes(), &respMsg)
c.Assert(reflect.DeepEqual(respMsg, testResp), IsTrue)
require.Equal(t, testResp, respMsg)
}

func (s *fnSuite) TestSetErrorEncoder(c *C) {
func TestSetErrorEncoder(t *testing.T) {
handler := Wrap(func(ctx context.Context, request *http.Request) (context.Context, error) {
return nil, errors.New("")
})
Expand All @@ -191,53 +183,50 @@ func (s *fnSuite) TestSetErrorEncoder(c *C) {

recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
c.Assert(err, IsNil)
require.NoError(t, err)
handler.ServeHTTP(recorder, request)

respMsg := &testErrorResponse{}
_ = json.Unmarshal(recorder.Body.Bytes(), &respMsg)
c.Assert(reflect.DeepEqual(respMsg, testErrorResp), IsTrue)
require.Equal(t, testErrorResp, respMsg)
}

func (s *fnSuite) TestGenericAdapter_Invoke(c *C) {
func TestGenericAdapter_Invoke(t *testing.T) {
type CustomForm testRequest
handler := Wrap(func(ctx context.Context, form *CustomForm) (context.Context, error) {
return nil, nil
})

recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
c.Assert(err == nil, IsTrue)
require.NoError(t, err)
payload := []byte(`{"for":"hello", "bar":10000}`)
request.Body = ioutil.NopCloser(bytes.NewBuffer(payload))
c.Assert(err, IsNil)
require.NoError(t, err)
handler.ServeHTTP(recorder, request)
}

func (s *fnSuite) TestSimpleUnaryAdapter_Invoke(c *C) {
func TestSimpleUnaryAdapter_Invoke(t *testing.T) {
handler := Wrap(withReq)

recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
if err != nil {
c.Fatal(err)
}
require.NoError(t, err)
payload := []byte(`{"for":"hello", "bar":10000}`)
request.Body = ioutil.NopCloser(bytes.NewBuffer(payload))
c.Assert(err, IsNil)
require.NoError(t, err)
handler.ServeHTTP(recorder, request)
}

func (s *fnSuite) TestErrorWithStatusCode(c *C) {
func TestErrorWithStatusCode(t *testing.T) {
handler := Wrap(func(ctx context.Context, request *http.Request) (context.Context, error) {
return nil, ErrorWithStatusCode(errors.New("not found"), http.StatusNotFound)
})

recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "", nil)
c.Assert(err, IsNil)
require.NoError(t, err)
handler.ServeHTTP(recorder, request)
c.Assert(recorder.Code == http.StatusNotFound, IsTrue)
require.Equal(t, http.StatusNotFound, recorder.Code)
}

func BenchmarkSimplePlainAdapter_Invoke(b *testing.B) {
Expand Down