Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Sep 30, 2023
1 parent cb07b82 commit 5c5fe12
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 4 deletions.
49 changes: 49 additions & 0 deletions example/simple/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,55 @@ func main() {
}
log.Printf("Greeting: %s (return code %d)", r.Message, r.ReturnCode)

md2 := metadata.New(map[string]string{"Authorization": "Basic dXNlcjp1c2Vy", "ab": "blue"})
ctx = metadata.NewOutgoingContext(context.Background(), md2)

var headers2 metadata.MD

name = "simple3"
r, err = c.SayHello(ctx, &pb.Request{Name: name}, grpc.Header(&headers2))
if err != nil {
log.Fatalf("error from grpc: %v", err)
}
if r.ReturnCode != 0 {
log.Fatalf("grpc server returned code: %d, expected code: %d", r.ReturnCode, 0)
}
if _, ok := headers2["result"]; !ok {
log.Fatal("header key `result` not found")
}
if len(headers2["result"]) != 3 {
log.Fatalf("the service did not return headers %+v", headers2)
}
if headers2["result"][0] != "blue" && headers2["result"][1] != "red" && headers2["result"][2] != "none" {
log.Fatal("the service returned an incorrect header")
}
log.Printf("Greeting: %s (return code %d)", r.Message, r.ReturnCode)

md3 := metadata.New(map[string]string{"Authorization": "Basic dXNlcjp1c2Vy", "ab": "red"})
ctx = metadata.NewOutgoingContext(context.Background(), md3)

var headers3 metadata.MD

name = "simple3"
r, err = c.SayHello(ctx, &pb.Request{Name: name}, grpc.Header(&headers3))
if err != nil {
log.Fatalf("error from grpc: %v", err)
}
if r.ReturnCode != 0 {
log.Fatalf("grpc server returned code: %d, expected code: %d", r.ReturnCode, 0)
}
if _, ok := headers3["result"]; !ok {
log.Fatal("header key `result` not found")
}
headers3.Get("result")
if len(headers3["result"]) != 3 {
log.Fatalf("the service did not return headers %+v", headers3)
}
if headers2["result"][0] != "red" && headers2["result"][1] != "blue" && headers2["result"][2] != "none" {
log.Fatal("the service returned an incorrect header")
}
log.Printf("Greeting: %s (return code %d)", r.Message, r.ReturnCode)

name = "simple3"
r, err = c.SayHello(context.Background(), &pb.Request{Name: name}, grpc.UseCompressor(gzip.Name))
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions example/simple/stub/simple3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,31 @@
message: Authorization OK
headers:
result: ok
- service: Gripmock
method: SayHello
headers:
contains:
authorization: Basic dXNlcjp1c2Vy
ab: blue
input:
equals:
name: simple3
output:
data:
message: Blue OK
headers:
result: blue;red;none
- service: Gripmock
method: SayHello
headers:
contains:
authorization: Basic dXNlcjp1c2Vy
ab: red
input:
equals:
name: simple3
output:
data:
message: Red OK
headers:
result: red;blue;none
4 changes: 2 additions & 2 deletions gripmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
func main() {
outputPointer := flag.String("o", "", "directory to output server.go. Default is $GOPATH/src/grpc/")
grpcPort := flag.String("grpc-port", "4770", "Port of gRPC tcp server")
grpcBindAddr := flag.String("grpc-listen", "", "Adress the gRPC server will bind to. Default to localhost, set to 0.0.0.0 to use from another machine")
grpcBindAddr := flag.String("grpc-listen", "0.0.0.0", "Adress the gRPC server will bind to. Default to localhost, set to 0.0.0.0 to use from another machine")
adminport := flag.String("admin-port", "4771", "Port of stub admin server")
adminBindAddr := flag.String("admin-listen", "", "Adress the admin server will bind to. Default to localhost, set to 0.0.0.0 to use from another machine")
adminBindAddr := flag.String("admin-listen", "0.0.0.0", "Adress the admin server will bind to. Default to localhost, set to 0.0.0.0 to use from another machine")
stubPath := flag.String("stub", "", "Path where the stub files are (Optional)")
imports := flag.String("imports", "/protobuf", "comma separated imports path. default path /protobuf is where gripmock Dockerfile install WKT protos")
// for backwards compatibility
Expand Down
11 changes: 10 additions & 1 deletion protoc-gen-gripmock/server.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,16 @@ func findStub(ctx context.Context, service, method string, md metadata.MD, in, o
return err
}

mdResp := metadata.New(searchStub.JSON200.Headers)
mdResp := make(metadata.MD, len(searchStub.JSON200.Headers))
for k, v := range searchStub.JSON200.Headers {
splits := strings.Split(v, ";")
for i, s := range splits {
splits[i] = strings.TrimSpace(s)
}

mdResp[k] = splits
}

grpc.SetHeader(ctx, mdResp)

return jsonpb.Unmarshal(data, out)
Expand Down
3 changes: 2 additions & 1 deletion stub/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stub
import (
"fmt"
"log"
"net"
"net/http"
"os"

Expand All @@ -25,7 +26,7 @@ func RunRestServer(ch chan struct{}, opt Options) {
if opt.Port == "" {
opt.Port = DefaultPort
}
addr := opt.BindAddr + ":" + opt.Port
addr := net.JoinHostPort(opt.BindAddr, opt.Port)

apiServer, _ := app.NewRestServer(opt.StubPath)

Expand Down

0 comments on commit 5c5fe12

Please sign in to comment.