-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove returning error of Write* functions for ghttp.Server; add UT c…
…ases for gclient.Client.DoRequestObj
- Loading branch information
Showing
7 changed files
with
176 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\"}") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.