Seperate setup from test, update ctx test to be more generic
This commit is contained in:
Родитель
2182e790ce
Коммит
2c52450752
|
@ -4,14 +4,8 @@ package handler
|
|||
// implementation. It also includes service middlewares.
|
||||
|
||||
import (
|
||||
_ "errors"
|
||||
_ "time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
_ "github.com/go-kit/kit/log"
|
||||
_ "github.com/go-kit/kit/metrics"
|
||||
|
||||
pb "github.com/TuneLab/go-truss/truss/_integration-tests/transport/transport-service"
|
||||
)
|
||||
|
||||
|
@ -54,10 +48,10 @@ func (s transportService) PostWithNestedMessageBody(ctx context.Context, in *pb.
|
|||
return &response, nil
|
||||
}
|
||||
|
||||
// CtxtToCtxtViaHTTPHeader implements Service.
|
||||
func (s transportService) CtxToCtxViaHTTPHeader(ctx context.Context, in *pb.HeaderRequest) (*pb.HeaderResponse, error) {
|
||||
var resp pb.HeaderResponse
|
||||
val := ctx.Value(in.HeaderKey)
|
||||
// CtxToCtx implements Service.
|
||||
func (s transportService) CtxToCtx(ctx context.Context, in *pb.MetaRequest) (*pb.MetaResponse, error) {
|
||||
var resp pb.MetaResponse
|
||||
val := ctx.Value(in.Key)
|
||||
|
||||
if v, ok := val.(string); ok {
|
||||
resp.V = v
|
||||
|
@ -74,5 +68,5 @@ type Service interface {
|
|||
GetWithQuery(ctx context.Context, in *pb.GetWithQueryRequest) (*pb.GetWithQueryResponse, error)
|
||||
GetWithRepeatedQuery(ctx context.Context, in *pb.GetWithRepeatedQueryRequest) (*pb.GetWithRepeatedQueryResponse, error)
|
||||
PostWithNestedMessageBody(ctx context.Context, in *pb.PostWithNestedMessageBodyRequest) (*pb.PostWithNestedMessageBodyResponse, error)
|
||||
CtxToCtxViaHTTPHeader(ctx context.Context, in *pb.HeaderRequest) (*pb.HeaderResponse, error)
|
||||
CtxToCtx(ctx context.Context, in *pb.MetaRequest) (*pb.MetaResponse, error)
|
||||
}
|
||||
|
|
|
@ -8,53 +8,19 @@ import (
|
|||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
|
||||
// 3d Party
|
||||
"golang.org/x/net/context"
|
||||
|
||||
// Go Kit
|
||||
"github.com/go-kit/kit/log"
|
||||
|
||||
// This Service
|
||||
pb "github.com/TuneLab/go-truss/truss/_integration-tests/transport/transport-service"
|
||||
svc "github.com/TuneLab/go-truss/truss/_integration-tests/transport/transport-service/generated"
|
||||
httpclient "github.com/TuneLab/go-truss/truss/_integration-tests/transport/transport-service/generated/client/http"
|
||||
handler "github.com/TuneLab/go-truss/truss/_integration-tests/transport/transport-service/handlers/server"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var httpserver *httptest.Server
|
||||
|
||||
func init() {
|
||||
var logger log.Logger
|
||||
logger = log.NewNopLogger()
|
||||
|
||||
var service handler.Service
|
||||
{
|
||||
service = handler.NewService()
|
||||
}
|
||||
|
||||
// Endpoint domain.
|
||||
getWithQueryE := svc.MakeGetWithQueryEndpoint(service)
|
||||
getWithRepeatedQueryE := svc.MakeGetWithRepeatedQueryEndpoint(service)
|
||||
postWithNestedMessageBodyE := svc.MakePostWithNestedMessageBodyEndpoint(service)
|
||||
ctxToCtxViaHTTPHeaderE := svc.MakeCtxToCtxViaHTTPHeaderEndpoint(service)
|
||||
|
||||
endpoints := svc.Endpoints{
|
||||
GetWithQueryEndpoint: getWithQueryE,
|
||||
GetWithRepeatedQueryEndpoint: getWithRepeatedQueryE,
|
||||
PostWithNestedMessageBodyEndpoint: postWithNestedMessageBodyE,
|
||||
CtxToCtxViaHTTPHeaderEndpoint: ctxToCtxViaHTTPHeaderE,
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
h := svc.MakeHTTPHandler(ctx, endpoints, logger)
|
||||
httpserver = httptest.NewServer(h)
|
||||
}
|
||||
// httpTestServer
|
||||
|
||||
func TestGetWithQueryClient(t *testing.T) {
|
||||
var req pb.GetWithQueryRequest
|
||||
|
@ -62,7 +28,7 @@ func TestGetWithQueryClient(t *testing.T) {
|
|||
req.B = 45360
|
||||
want := req.A + req.B
|
||||
|
||||
svchttp, err := httpclient.New(httpserver.URL)
|
||||
svchttp, err := httpclient.New(httpTestServer.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create httpclient: %q", err)
|
||||
}
|
||||
|
@ -113,7 +79,7 @@ func TestGetWithRepeatedQueryClient(t *testing.T) {
|
|||
req.A = []int64{12, 45360}
|
||||
want := req.A[0] + req.A[1]
|
||||
|
||||
svchttp, err := httpclient.New(httpserver.URL)
|
||||
svchttp, err := httpclient.New(httpTestServer.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create httpclient: %q", err)
|
||||
}
|
||||
|
@ -171,7 +137,7 @@ func TestPostWithNestedMessageBodyClient(t *testing.T) {
|
|||
req.NM = &reqNM
|
||||
want := req.NM.A + req.NM.B
|
||||
|
||||
svchttp, err := httpclient.New(httpserver.URL)
|
||||
svchttp, err := httpclient.New(httpTestServer.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create httpclient: %q", err)
|
||||
}
|
||||
|
@ -220,12 +186,12 @@ func TestPostWithNestedMessageBodyRequest(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestCtxToCtxViaHTTPHeaderClient(t *testing.T) {
|
||||
var req pb.HeaderRequest
|
||||
var req pb.MetaRequest
|
||||
var key, value = "Truss-Auth-Header", "SECRET"
|
||||
req.HeaderKey = key
|
||||
req.Key = key
|
||||
|
||||
// Create a new client telling it to send "Truss-Auth-Header" as a header
|
||||
svchttp, err := httpclient.New(httpserver.URL,
|
||||
svchttp, err := httpclient.New(httpTestServer.URL,
|
||||
httpclient.CtxValuesToSend(key))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create httpclient: %q", err)
|
||||
|
@ -235,7 +201,7 @@ func TestCtxToCtxViaHTTPHeaderClient(t *testing.T) {
|
|||
ctx := context.WithValue(context.Background(), key, value)
|
||||
|
||||
// send the context
|
||||
resp, err := svchttp.CtxToCtxViaHTTPHeader(ctx, &req)
|
||||
resp, err := svchttp.CtxToCtx(ctx, &req)
|
||||
if err != nil {
|
||||
t.Fatalf("httpclient returned error: %q", err)
|
||||
}
|
||||
|
@ -246,13 +212,13 @@ func TestCtxToCtxViaHTTPHeaderClient(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestCtxToCtxViaHTTPHeaderRequest(t *testing.T) {
|
||||
var resp pb.HeaderResponse
|
||||
var resp pb.MetaResponse
|
||||
var key, value = "Truss-Auth-Header", "SECRET"
|
||||
|
||||
jsonStr := fmt.Sprintf(`{ "HeaderKey": %q }`, key)
|
||||
jsonStr := fmt.Sprintf(`{ "Key": %q }`, key)
|
||||
fmt.Println(jsonStr)
|
||||
|
||||
req, err := http.NewRequest("POST", httpserver.URL+"/"+"ctxtoctx", strings.NewReader(jsonStr))
|
||||
req, err := http.NewRequest("POST", httpTestServer.URL+"/"+"ctxtoctx", strings.NewReader(jsonStr))
|
||||
if err != nil {
|
||||
t.Fatal(errors.Wrap(err, "cannot construct http request"))
|
||||
}
|
||||
|
@ -285,7 +251,7 @@ type httpRequestBuilder struct {
|
|||
|
||||
func (h httpRequestBuilder) Test(t *testing.T) ([]byte, error) {
|
||||
t.Logf("Method: %q | Route: %q", h.method, h.route)
|
||||
httpReq, err := http.NewRequest(h.method, httpserver.URL+"/"+h.route, bytes.NewReader(h.body))
|
||||
httpReq, err := http.NewRequest(h.method, httpTestServer.URL+"/"+h.route, bytes.NewReader(h.body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
// Go Kit
|
||||
"github.com/go-kit/kit/log"
|
||||
|
||||
svc "github.com/TuneLab/go-truss/truss/_integration-tests/transport/transport-service/generated"
|
||||
handler "github.com/TuneLab/go-truss/truss/_integration-tests/transport/transport-service/handlers/server"
|
||||
)
|
||||
|
||||
var httpTestServer *httptest.Server
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var logger log.Logger
|
||||
logger = log.NewNopLogger()
|
||||
|
||||
var service handler.Service
|
||||
{
|
||||
service = handler.NewService()
|
||||
}
|
||||
|
||||
// Endpoint domain.
|
||||
getWithQueryE := svc.MakeGetWithQueryEndpoint(service)
|
||||
getWithRepeatedQueryE := svc.MakeGetWithRepeatedQueryEndpoint(service)
|
||||
postWithNestedMessageBodyE := svc.MakePostWithNestedMessageBodyEndpoint(service)
|
||||
ctxToCtxE := svc.MakeCtxToCtxEndpoint(service)
|
||||
|
||||
endpoints := svc.Endpoints{
|
||||
GetWithQueryEndpoint: getWithQueryE,
|
||||
GetWithRepeatedQueryEndpoint: getWithRepeatedQueryE,
|
||||
PostWithNestedMessageBodyEndpoint: postWithNestedMessageBodyE,
|
||||
CtxToCtxEndpoint: ctxToCtxE,
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
h := svc.MakeHTTPHandler(ctx, endpoints, logger)
|
||||
|
||||
httpTestServer = httptest.NewServer(h)
|
||||
|
||||
os.Exit(m.Run())
|
||||
}
|
|
@ -21,7 +21,7 @@ service TransportPermutations {
|
|||
body: "*"
|
||||
};
|
||||
}
|
||||
rpc CtxToCtxViaHTTPHeader (HeaderRequest) returns (HeaderResponse){
|
||||
rpc CtxToCtx (MetaRequest) returns (MetaResponse){
|
||||
option (google.api.http) = {
|
||||
post: "/ctxtoctx"
|
||||
body: "*"
|
||||
|
@ -59,10 +59,10 @@ message PostWithNestedMessageBodyResponse {
|
|||
int64 V = 1;
|
||||
}
|
||||
|
||||
message HeaderRequest{
|
||||
string HeaderKey = 1;
|
||||
message MetaRequest{
|
||||
string Key = 1;
|
||||
}
|
||||
|
||||
message HeaderResponse {
|
||||
message MetaResponse {
|
||||
string V = 1;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче