Skip to content

Commit

Permalink
👸 Add proto file input to jig (#41)
Browse files Browse the repository at this point in the history
Add proto source file (.proto) input to jig serve and jig bones, so that
the user doesn't have to pre-compile FileDescriptorSets.

Extend tests run httprule serve via proto files end-to-end.

This merges the following commits:
* Upgrade to go 1.18
* Narrow up-to-date check to *.pb and go.* files
* Read input from --proto flags
* Add proto file input to jig bones

     Makefile                                      |  2 +-
     bin/{.go-1.17.3.pkg => .go-1.18.pkg}          |  0
     ...t-1.43.0.pkg => .golangci-lint-1.45.2.pkg} |  0
     bin/go                                        |  2 +-
     bin/gofmt                                     |  2 +-
     bin/golangci-lint                             |  2 +-
     bones/generate.go                             | 22 +------
     bones/generate_test.go                        | 17 ++++-
     go.mod                                        | 10 +--
     go.sum                                        | 22 ++++---
     main.go                                       | 62 ++++++++++++++---
     main_test.go                                  | 51 ++++++++++++++
     serve/httprule/server.go                      | 10 +--
     serve/httprule/server_test.go                 |  3 +-
     serve/server.go                               | 66 +++++++++++++------
     .../httpgreet.HttpGreeter.GetHello.jsonnet    | 15 +++++
     .../httpgreet.HttpGreeter.PostHello.jsonnet   | 15 +++++
     ...httpgreet.HttpGreeter.PostHelloURL.jsonnet | 15 +++++
     18 files changed, 241 insertions(+), 75 deletions(-)

Pull-Request: #41
  • Loading branch information
juliaogris committed Apr 11, 2022
2 parents a8a0a65 + 39b3cc4 commit be4c9bb
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ all: build test check-coverage lint lint-proto ## build, test, check coverage an
ci: clean check-uptodate all ## Full clean build and up-to-date checks as run on CI

check-uptodate: proto tidy
test -z "$$(git status --porcelain)" || { git status; false; }
test -z "$$(git status --porcelain -- go.mod go.sum '*.pb')" || { git status; false; }

clean:: ## Remove generated files
-rm -rf $(O)
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion bin/go
2 changes: 1 addition & 1 deletion bin/gofmt
2 changes: 1 addition & 1 deletion bin/golangci-lint
22 changes: 2 additions & 20 deletions bones/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"os"
"path"

"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
)

Expand All @@ -17,8 +15,8 @@ import (
// slice. Method exemplars are written to stdout if methodDir is empty,
// otherwise each method is written to a separate file in that directory.
// Existing files will not be overwritten unless force is true.
func Generate(protoSet, methodDir string, force bool, targets []string, formatter FormatOptions) error {
files, err := loadProtoSet(protoSet)
func Generate(fds *descriptorpb.FileDescriptorSet, methodDir string, force bool, targets []string, formatter FormatOptions) error {
files, err := protodesc.NewFiles(fds)
if err != nil {
return err
}
Expand All @@ -31,22 +29,6 @@ func Generate(protoSet, methodDir string, force bool, targets []string, formatte
return err
}

func loadProtoSet(protoSet string) (*protoregistry.Files, error) {
b, err := os.ReadFile(protoSet)
if err != nil {
return nil, err
}
fds := &descriptorpb.FileDescriptorSet{}
if err := proto.Unmarshal(b, fds); err != nil {
return nil, err
}
files, err := protodesc.NewFiles(fds)
if err != nil {
return nil, err
}
return files, nil
}

func genFile(fd protoreflect.FileDescriptor, methodDir string, force bool, targets []string, formatter FormatOptions) error {
for _, sd := range services(fd) {
for _, md := range methods(sd) {
Expand Down
17 changes: 15 additions & 2 deletions bones/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"

"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
)

func TestGenerateGolden(t *testing.T) {
Expand All @@ -23,15 +25,26 @@ func TestGenerateGolden(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
dir := t.TempDir()
err := Generate(tc.pbFile, dir, false, nil, FormatOptions{QuoteStyle: tc.quoteStyle})

err := Generate(getFDS(t, tc.pbFile), dir, false, nil, FormatOptions{QuoteStyle: tc.quoteStyle})
require.NoError(t, err)
err = Generate(tc.pbFile, dir, false, nil, FormatOptions{Lang: JS, QuoteStyle: tc.quoteStyle})
err = Generate(getFDS(t, tc.pbFile), dir, false, nil, FormatOptions{Lang: JS, QuoteStyle: tc.quoteStyle})
require.NoError(t, err)
requireSameContent(t, tc.goldenDir, dir)
})
}
}

func getFDS(t *testing.T, filename string) *descriptorpb.FileDescriptorSet {
t.Helper()
b, err := os.ReadFile(filename)
require.NoError(t, err)
fds := &descriptorpb.FileDescriptorSet{}
err = proto.Unmarshal(b, fds)
require.NoError(t, err)
return fds
}

func requireSameContent(t *testing.T, wantDir, gotDir string) {
t.Helper()
wantFiles, err := os.ReadDir(wantDir)
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
module foxygo.at/jig

go 1.17
go 1.18

require (
foxygo.at/protog v0.0.14
github.com/alecthomas/kong v0.4.1
github.com/alecthomas/protobuf v0.0.0-20220411053735-2a9f60104b87
github.com/google/go-jsonnet v0.17.0
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/genproto v0.0.0-20211129164237-f09f9a12af12
google.golang.org/grpc v1.42.0
google.golang.org/protobuf v1.27.1
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac
google.golang.org/grpc v1.45.0
google.golang.org/protobuf v1.28.0
)

require (
github.com/alecthomas/participle/v2 v2.0.0-alpha7 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
22 changes: 12 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
foxygo.at/protog v0.0.14 h1:A8wnusX0iDVDqfuD8XXQxTJyZvqD0lwApQf2L29uqGc=
foxygo.at/protog v0.0.14/go.mod h1:z0MaC+G1OT/hhL1rstKjhqCGT7AeyG0kmWxZQDwIh8w=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alecthomas/kong v0.4.1 h1:0sFnMts+ijOiFuSHsMB9MlDi3NGINBkx9KIw1/gcuDw=
github.com/alecthomas/kong v0.4.1/go.mod h1:uzxf/HUh0tj43x1AyJROl3JT7SgsZ5m+icOv1csRhc0=
github.com/alecthomas/participle/v2 v2.0.0-alpha7 h1:cK4vjj0VSgb3lN1nuKA5F7dw+1s1pWBe5bx7nNCnN+c=
github.com/alecthomas/participle/v2 v2.0.0-alpha7/go.mod h1:NumScqsC42o9x+dGj8/YqsIfhrIQjFEOFovxotbBirA=
github.com/alecthomas/protobuf v0.0.0-20220411053735-2a9f60104b87 h1:m3konG8+IB1wCqdE+xNT746VEX5MUUngM9ppz4ov+kc=
github.com/alecthomas/protobuf v0.0.0-20220411053735-2a9f60104b87/go.mod h1:HDvXxy4Xhq9USJZ8N+NbFhzOCvndYPf0wY3uB9CK4xA=
github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ=
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
Expand Down Expand Up @@ -53,8 +56,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-jsonnet v0.17.0 h1:/9NIEfhK1NQRKl3sP2536b2+x5HnZMdql7x3yK/l8JY=
github.com/google/go-jsonnet v0.17.0/go.mod h1:sOcuej3UW1vpPTZOr8L7RQimqai1a57bt5j22LzGZCw=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand All @@ -75,7 +78,6 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
Expand Down Expand Up @@ -146,8 +148,8 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20210805201207-89edb61ffb67/go.mod h1:ob2IJxKrgPT52GcgX759i1sleT07tiKowYBGbczaW48=
google.golang.org/genproto v0.0.0-20211129164237-f09f9a12af12 h1:DN5b3HU13J4sMd/QjDx34U6afpaexKTDdop+26pdjdk=
google.golang.org/genproto v0.0.0-20211129164237-f09f9a12af12/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac h1:qSNTkEN+L2mvWcLgJOR+8bdHX9rN/IdU3A1Ghpfb1Rg=
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
Expand All @@ -156,9 +158,8 @@ google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTp
google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
google.golang.org/grpc v1.39.0/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE=
google.golang.org/grpc v1.39.1/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE=
google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34=
google.golang.org/grpc v1.42.0 h1:XT2/MFpuPFsEX2fWh3YQtHkZ+WYZFQRfaUgLZYj/p6A=
google.golang.org/grpc v1.42.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU=
google.golang.org/grpc v1.45.0 h1:NEpgUqV3Z+ZjkqMsxMg11IaDrXY4RY6CQukSGK0uI1M=
google.golang.org/grpc v1.45.0/go.mod h1:lN7owxKUQEqMfSyQikvvk5tf/6zMPsrK+ONuO11+0rQ=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand All @@ -170,8 +171,9 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
62 changes: 54 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package main

import (
"fmt"
"os"

"foxygo.at/jig/bones"
"foxygo.at/jig/log"
"foxygo.at/jig/serve"
"foxygo.at/jig/serve/httprule"
"github.com/alecthomas/kong"
"github.com/alecthomas/protobuf/compiler"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
)

var version = "v0.0.0"
Expand All @@ -19,16 +23,24 @@ type config struct {
}

type cmdServe struct {
ProtoSet []string `short:"p" help:"Protoset .pb files containing service and deps"`
ProtoSet []string `short:"p" help:"Protoset .pb files containing service and deps"`

Proto []string `short:"P" help:"Proto source .proto files containing service"`
ProtoPath []string `short:"I" help:"Import paths for --proto files' dependencies"`

LogLevel log.LogLevel `help:"Server logging level" default:"error"`
Listen string `short:"l" default:"localhost:8080" help:"TCP listen address"`
HTTP bool `short:"h" help:"Serve on HTTP too, using HttpRule annotations"`

Dirs []string `arg:"" help:"Directory containing method definitions and protoset .pb file"`
Dirs []string `arg:"" help:"Directory containing method definitions and optionally protoset .pb file"`
}

type cmdBones struct {
ProtoSet string `short:"p" help:"Protoset .pb file containing service and deps" required:""`
ProtoSet string `short:"p" help:"Protoset .pb file containing service and deps" xor:"proto"`

Proto string `short:"P" help:"Proto source .proto file containing service" xor:"proto"`
ProtoPath []string `short:"I" help:"Import paths for --proto files' dependencies"`

MethodDir string `short:"m" help:"Directory to write method definitions to"`
Force bool `short:"f" help:"Overwrite existing bones files"`
Targets []string `arg:"" optional:"" help:"Target pkg/service/method to generate"`
Expand All @@ -45,26 +57,60 @@ func main() {
}

func (cs *cmdServe) Run() error {
withLogger := serve.WithLogger(log.NewLogger(os.Stderr, cs.LogLevel))
withProtosets := serve.WithProtosets(cs.ProtoSet...)
logger := log.NewLogger(os.Stderr, cs.LogLevel)
opts, err := cs.getServerOptions(logger)
if err != nil {
return err
}
dirs := serve.NewFSFromDirs(cs.Dirs...)
s, err := serve.NewServer(serve.JsonnetEvaluator(), dirs, withLogger, withProtosets)
s, err := serve.NewServer(serve.JsonnetEvaluator(), dirs, opts...)
if err != nil {
return err
}

if cs.HTTP {
h := httprule.NewServer(s.Files, s.UnknownHandler)
h := httprule.NewServer(s.Files, s.UnknownHandler, logger)
s.SetHTTPHandler(h)
}

return s.ListenAndServe(cs.Listen)
}

func (cs *cmdServe) getServerOptions(logger log.Logger) ([]serve.Option, error) {
opts := []serve.Option{serve.WithLogger(logger), serve.WithProtosets(cs.ProtoSet...)}
if len(cs.Proto) != 0 {
includeImports := true
fds, err := compiler.Compile(cs.Proto, cs.ProtoPath, includeImports)
if err != nil {
return nil, fmt.Errorf("cannot compile protos %v with import paths %v: %w", cs.Proto, cs.ProtoPath, err)
}
opts = append(opts, serve.WithFileDescriptorSets(fds))
}
return opts, nil
}

func (cb *cmdBones) Run() error {
fds := &descriptorpb.FileDescriptorSet{}
if cb.ProtoSet != "" {
b, err := os.ReadFile(cb.ProtoSet)
if err != nil {
return err
}
if err := proto.Unmarshal(b, fds); err != nil {
return err
}
} else {
var err error
includeImports := true
fds, err = compiler.Compile([]string{cb.Proto}, cb.ProtoPath, includeImports)
if err != nil {
return fmt.Errorf("cannot compile protos %v with import paths %v: %w", cb.Proto, cb.ProtoPath, err)
}
}

opts := bones.FormatOptions{
Lang: cb.Language,
QuoteStyle: cb.QuoteStyle,
}
return bones.Generate(cb.ProtoSet, cb.MethodDir, cb.Force, cb.Targets, opts)
return bones.Generate(fds, cb.MethodDir, cb.Force, cb.Targets, opts)
}
51 changes: 51 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"io"
"net/http"
"os"
"strings"
"testing"

"foxygo.at/jig/log"
"foxygo.at/jig/serve"
"foxygo.at/jig/serve/httprule"
"github.com/stretchr/testify/require"
)

func TestHTTPRuleServer(t *testing.T) {
c := cmdServe{
Proto: []string{"httpgreet/httpgreet.proto"},
ProtoPath: []string{"proto"},
}
logger := log.NewLogger(io.Discard, log.LogLevelError)
opts, err := c.getServerOptions(logger)
require.NoError(t, err)

ts := serve.NewUnstartedTestServer(serve.JsonnetEvaluator(), os.DirFS("serve/testdata/httpgreet"), opts...)
ts.SetHTTPHandler(httprule.NewServer(ts.Files, ts.UnknownHandler, logger))
ts.Start()
defer ts.Stop()

baseURL := "http://" + ts.Addr()
resp, err := http.Get(baseURL + "/api/greet/hello/Dolly")
require.NoError(t, err)
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.JSONEq(t, `{"greeting":"httpgreet: Hello, Dolly"}`, string(b))

resp, err = http.Post(baseURL+"/api/greet/hello", "application/json", strings.NewReader(`{"firstName": "Kitty"}`))
require.NoError(t, err)
defer resp.Body.Close()
b, err = io.ReadAll(resp.Body)
require.NoError(t, err)
require.JSONEq(t, `{"greeting":"Thanks for the post, Kitty"}`, string(b))

resp, err = http.Post(baseURL+"/api/greet/world", "application/json", strings.NewReader(`{}`))
require.NoError(t, err)
defer resp.Body.Close()
b, err = io.ReadAll(resp.Body)
require.NoError(t, err)
require.JSONEq(t, `{"greeting":"Thanks for the post and the path, world"}`, string(b))
}
Loading

0 comments on commit be4c9bb

Please sign in to comment.