From b05760f863db44c550f11a5ccc0009416a7b5bdf Mon Sep 17 00:00:00 2001 From: Deng Ming Date: Sun, 11 Feb 2024 22:12:26 +0800 Subject: [PATCH] =?UTF-8?q?httptestx:=20=E6=94=AF=E6=8C=81=20JSONRecorder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {script => .script}/goimports.sh | 0 {script => .script}/setup.sh | 0 Makefile | 4 +-- net/httpx/httptestx/recorder.go | 44 ++++++++++++++++++++++++++++ net/httpx/httptestx/recorder_test.go | 40 +++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 2 deletions(-) rename {script => .script}/goimports.sh (100%) rename {script => .script}/setup.sh (100%) create mode 100644 net/httpx/httptestx/recorder.go create mode 100644 net/httpx/httptestx/recorder_test.go diff --git a/script/goimports.sh b/.script/goimports.sh similarity index 100% rename from script/goimports.sh rename to .script/goimports.sh diff --git a/script/setup.sh b/.script/setup.sh similarity index 100% rename from script/setup.sh rename to .script/setup.sh diff --git a/Makefile b/Makefile index c7e6056c..824788f7 100644 --- a/Makefile +++ b/Makefile @@ -8,11 +8,11 @@ ut: .PHONY: setup setup: - @sh ./script/setup.sh + @sh ./.script/setup.sh .PHONY: fmt fmt: - @sh ./script/goimports.sh + @sh ./.script/goimports.sh .PHONY: lint lint: diff --git a/net/httpx/httptestx/recorder.go b/net/httpx/httptestx/recorder.go new file mode 100644 index 00000000..1eee3562 --- /dev/null +++ b/net/httpx/httptestx/recorder.go @@ -0,0 +1,44 @@ +// Copyright 2021 ecodeclub +// +// 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://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 httptestx + +import ( + "encoding/json" + "net/http/httptest" +) + +type JSONResponseRecorder[T any] struct { + *httptest.ResponseRecorder +} + +func NewJSONResponseRecorder[T any]() *JSONResponseRecorder[T] { + return &JSONResponseRecorder[T]{ + ResponseRecorder: httptest.NewRecorder(), + } +} + +func (r JSONResponseRecorder[T]) Scan() (T, error) { + var t T + err := json.NewDecoder(r.Body).Decode(&t) + return t, err +} + +func (r JSONResponseRecorder[T]) MustScan() T { + t, err := r.Scan() + if err != nil { + panic(err) + } + return t +} diff --git a/net/httpx/httptestx/recorder_test.go b/net/httpx/httptestx/recorder_test.go new file mode 100644 index 00000000..5e21c5f7 --- /dev/null +++ b/net/httpx/httptestx/recorder_test.go @@ -0,0 +1,40 @@ +// Copyright 2021 ecodeclub +// +// 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://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 httptestx + +import ( + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "testing" +) + +func TestJSONResponseRecorder_MustScan(t *testing.T) { + // 成功案例 + recorder := NewJSONResponseRecorder[User]() + _, err := recorder.WriteString(`{"name": "Tom"}`) + require.NoError(t, err) + u := recorder.MustScan() + assert.Equal(t, User{Name: "Tom"}, u) + + // panic 案例 + recorder = NewJSONResponseRecorder[User]() + assert.Panics(t, func() { + recorder.MustScan() + }) +} + +type User struct { + Name string `json:"name"` +}