Skip to content

Commit

Permalink
remove returning error of Write* functions for ghttp.Server; add UT c…
Browse files Browse the repository at this point in the history
…ases for gclient.Client.DoRequestObj
  • Loading branch information
gqcn committed Jun 15, 2022
1 parent f4f73f2 commit 17ab0e2
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 104 deletions.
5 changes: 3 additions & 2 deletions net/gclient/gclient_z_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
"crypto/tls"
"encoding/hex"
"fmt"
"net/http"
"time"

"github.com/gogf/gf/v2/debug/gdebug"
"github.com/gogf/gf/v2/net/gclient"
"github.com/gogf/gf/v2/net/gtcp"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gfile"
"net/http"
"time"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
Expand Down
78 changes: 78 additions & 0 deletions net/gclient/gclient_z_unit_feature_trace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gclient_test

import (
"context"
"fmt"
"net/http"
"testing"
"time"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/tracing"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/net/gtcp"
"github.com/gogf/gf/v2/test/gtest"
"go.opentelemetry.io/otel"
sdkTrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)

type CustomProvider struct {
*sdkTrace.TracerProvider
}

func NewCustomProvider() *CustomProvider {
return &CustomProvider{
TracerProvider: sdkTrace.NewTracerProvider(
sdkTrace.WithIDGenerator(NewCustomIDGenerator()),
),
}
}

type CustomIDGenerator struct{}

func NewCustomIDGenerator() *CustomIDGenerator {
return &CustomIDGenerator{}
}

func (id *CustomIDGenerator) NewIDs(ctx context.Context) (traceID trace.TraceID, spanID trace.SpanID) {
return tracing.NewIDs()
}

func (id *CustomIDGenerator) NewSpanID(ctx context.Context, traceID trace.TraceID) (spanID trace.SpanID) {
return tracing.NewSpanID()
}

func TestClient_CustomProvider(t *testing.T) {
provider := otel.GetTracerProvider()
defer otel.SetTracerProvider(provider)

otel.SetTracerProvider(NewCustomProvider())

p, _ := gtcp.GetFreePort()
s := g.Server(p)
s.BindHandler("/hello", func(r *ghttp.Request) {
r.Response.WriteHeader(200)
r.Response.WriteJson(g.Map{"field": "test_for_response_body"})
})
s.SetPort(p)
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
c := g.Client()
url := fmt.Sprintf("127.0.0.1:%d/hello", p)
resp, err := c.DoRequest(ctx, http.MethodGet, url)
t.AssertNil(err)
t.AssertNE(resp, nil)
t.Assert(resp.ReadAllString(), "{\"field\":\"test_for_response_body\"}")
})
}
78 changes: 78 additions & 0 deletions net/gclient/gclient_z_unit_request_obj_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gclient_test

import (
"fmt"
"testing"
"time"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/net/gtcp"
"github.com/gogf/gf/v2/test/gtest"
)

func Test_Client_DoRequestObj(t *testing.T) {
type UserCreateReq struct {
g.Meta `path:"/user" method:"post"`
Id int
Name string
}
type UserCreateRes struct {
Id int
}
type UserQueryReq struct {
g.Meta `path:"/user" method:"get"`
}
type UserQueryRes struct {
Id int
Name string
}
p, _ := gtcp.GetFreePort()
s := g.Server(p)
s.Group("/user", func(group *ghttp.RouterGroup) {
group.GET("/", func(r *ghttp.Request) {
r.Response.WriteJson(g.Map{"id": 1, "name": "john"})
})
group.POST("/", func(r *ghttp.Request) {
r.Response.WriteJson(g.Map{"id": r.Get("Id")})
})
})
s.SetPort(p)
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
url := fmt.Sprintf("http://127.0.0.1:%d", p)
client := g.Client().SetPrefix(url).ContentJson()
var (
createRes *UserCreateRes
createReq = UserCreateReq{
Id: 1,
Name: "john",
}
)
err := client.DoRequestObj(ctx, createReq, &createRes)
t.AssertNil(err)
t.Assert(createRes.Id, 1)
})
gtest.C(t, func(t *gtest.T) {
url := fmt.Sprintf("http://127.0.0.1:%d", p)
client := g.Client().SetPrefix(url).ContentJson()
var (
queryRes *UserQueryRes
queryReq = UserQueryReq{}
)
err := client.DoRequestObj(ctx, queryReq, &queryRes)
t.AssertNil(err)
t.Assert(queryRes.Id, 1)
t.Assert(queryRes.Name, "john")
})
}
62 changes: 2 additions & 60 deletions net/gclient/gclient_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ import (
"context"
"crypto/tls"
"fmt"
"github.com/gogf/gf/v2/debug/gdebug"
"github.com/gogf/gf/v2/internal/tracing"
"go.opentelemetry.io/otel"
sdkTrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"io/ioutil"
"net/http"
"testing"
"time"

"github.com/gogf/gf/v2/debug/gdebug"

"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gclient"
Expand Down Expand Up @@ -643,58 +640,3 @@ func TestClient_RequestVar(t *testing.T) {
t.AssertNE(users, nil)
})
}

type CustomProvider struct {
*sdkTrace.TracerProvider
}

func NewCustomProvider() *CustomProvider {
return &CustomProvider{
TracerProvider: sdkTrace.NewTracerProvider(
sdkTrace.WithIDGenerator(NewCustomIDGenerator()),
),
}
}

type CustomIDGenerator struct{}

func NewCustomIDGenerator() *CustomIDGenerator {
return &CustomIDGenerator{}
}

func (id *CustomIDGenerator) NewIDs(ctx context.Context) (traceID trace.TraceID, spanID trace.SpanID) {
return tracing.NewIDs()
}

func (id *CustomIDGenerator) NewSpanID(ctx context.Context, traceID trace.TraceID) (spanID trace.SpanID) {
return tracing.NewSpanID()
}

func TestClient_CustomProvider(t *testing.T) {
provider := otel.GetTracerProvider()
defer otel.SetTracerProvider(provider)

otel.SetTracerProvider(NewCustomProvider())

p, _ := gtcp.GetFreePort()
s := g.Server(p)
s.BindHandler("/hello", func(r *ghttp.Request) {
r.Response.WriteHeader(200)
r.Response.WriteJson(g.Map{"field": "test_for_response_body"})
})
s.SetPort(p)
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
c := g.Client()
url := fmt.Sprintf("127.0.0.1:%d/hello", p)
resp, err := c.DoRequest(ctx, http.MethodGet, url)
t.AssertNil(err)
t.AssertNE(resp, nil)
t.Assert(resp.ReadAllString(), "{\"field\":\"test_for_response_body\"}")
})

}
7 changes: 1 addition & 6 deletions net/ghttp/ghttp_middleware_handler_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/intlog"
)

// DefaultHandlerResponse is the default implementation of HandlerResponse.
Expand All @@ -32,7 +31,6 @@ func MiddlewareHandlerResponse(r *Request) {

var (
msg string
ctx = r.Context()
err = r.GetError()
res = r.GetHandlerResponse()
code = gerror.Code(err)
Expand All @@ -55,12 +53,9 @@ func MiddlewareHandlerResponse(r *Request) {
} else {
code = gcode.CodeOK
}
internalErr := r.Response.WriteJson(DefaultHandlerResponse{
r.Response.WriteJson(DefaultHandlerResponse{
Code: code.Code(),
Message: msg,
Data: res,
})
if internalErr != nil {
intlog.Errorf(ctx, `%+v`, internalErr)
}
}
Loading

0 comments on commit 17ab0e2

Please sign in to comment.