Skip to content

Commit 2b884e3

Browse files
Merge pull request #27 from speakeasy-api/add-openapi-support
feat: add support for retrieving path hints from an openapi document
2 parents 3731d1a + 6b85075 commit 2b884e3

File tree

9 files changed

+201
-22
lines changed

9 files changed

+201
-22
lines changed

.github/workflows/lint.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
steps:
1515
- uses: actions/setup-go@v3
1616
with:
17-
go-version: 1.17
17+
go-version: 1.18
1818

1919
- name: Configure git for private modules
2020
env:
@@ -27,3 +27,4 @@ jobs:
2727
with:
2828
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
2929
version: latest
30+
args: --timeout 5m

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
fail-fast: true
1616
matrix:
17-
go-version: [1.17.x, 1.18.x, 1.19.x]
17+
go-version: [1.18.x, 1.19.x, 1.20.x]
1818

1919
name: Tests - Go ${{ matrix.go-version }}
2020

capturewriter.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import (
77
)
88

99
type captureWriter struct {
10-
reqW *requestWriter
11-
origResW http.ResponseWriter
12-
resW *responseWriter
13-
reqBuf *bytes.Buffer
14-
resBuf *bytes.Buffer
15-
reqValid bool
16-
resValid bool
17-
status int
18-
responseSize int
19-
maxBuffer int
10+
reqW *requestWriter
11+
origResW http.ResponseWriter
12+
resW *responseWriter
13+
reqBuf *bytes.Buffer
14+
resBuf *bytes.Buffer
15+
reqValid bool
16+
resValid bool
17+
status int
18+
statusWritten bool
19+
responseSize int
20+
maxBuffer int
2021
}
2122

2223
func NewCaptureWriter(origResW http.ResponseWriter, maxBuffer int) *captureWriter {
@@ -112,8 +113,11 @@ func (c *captureWriter) writeRes(p []byte) (int, error) {
112113
}
113114

114115
func (c *captureWriter) writeHeader(statusCode int) {
115-
c.status = statusCode
116-
c.origResW.WriteHeader(statusCode)
116+
if !c.statusWritten {
117+
c.status = statusCode
118+
c.statusWritten = true
119+
c.origResW.WriteHeader(statusCode)
120+
}
117121
}
118122

119123
type requestWriter struct {

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/speakeasy-api/speakeasy-go-sdk
22

3-
go 1.17
3+
go 1.18
44

55
require (
66
github.com/chromedp/cdproto v0.0.0-20220629234738-4cfc9cdeeb92
@@ -31,6 +31,7 @@ require (
3131
github.com/cespare/xxhash/v2 v2.1.2 // indirect
3232
github.com/davecgh/go-spew v1.1.1 // indirect
3333
github.com/dgraph-io/ristretto v0.1.0 // indirect
34+
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
3435
github.com/dustin/go-humanize v1.0.0 // indirect
3536
github.com/felixge/httpsnoop v1.0.1 // indirect
3637
github.com/gin-contrib/sse v0.1.0 // indirect
@@ -50,21 +51,26 @@ require (
5051
github.com/mattn/go-isatty v0.0.14 // indirect
5152
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
5253
github.com/modern-go/reflect2 v1.0.2 // indirect
54+
github.com/pb33f/libopenapi v0.8.1 // indirect
55+
github.com/pb33f/libopenapi-validator v0.0.7 // indirect
5356
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
5457
github.com/philhofer/fwd v1.1.1 // indirect
5558
github.com/pkg/errors v0.9.1 // indirect
5659
github.com/pmezard/go-difflib v1.0.0 // indirect
60+
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0 // indirect
5761
github.com/secure-systems-lab/go-securesystemslib v0.4.0 // indirect
5862
github.com/tinylib/msgp v1.1.6 // indirect
5963
github.com/ugorji/go/codec v1.2.7 // indirect
6064
github.com/valyala/bytebufferpool v1.0.0 // indirect
6165
github.com/valyala/fasttemplate v1.2.1 // indirect
66+
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
6267
go.uber.org/atomic v1.7.0 // indirect
6368
go.uber.org/multierr v1.6.0 // indirect
6469
go4.org/intern v0.0.0-20211027215823-ae77deb06f29 // indirect
6570
go4.org/unsafe/assume-no-moving-gc v0.0.0-20220617031537-928513b29760 // indirect
6671
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
6772
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
73+
golang.org/x/sync v0.1.0 // indirect
6874
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
6975
golang.org/x/text v0.3.7 // indirect
7076
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUn
142142
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
143143
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
144144
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
145+
github.com/dprotaso/go-yit v0.0.0-20191028211022-135eb7262960/go.mod h1:9HQzr9D/0PGwMEbC3d5AB7oi67+h4TsQqItC1GVYG58=
146+
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 h1:PRxIJD8XjimM5aTknUK9w6DHLDox2r2M3DI4i2pnd3w=
147+
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936/go.mod h1:ttYvX5qlB+mlV1okblJqcSMtR4c52UKxDiX9GRBS8+Q=
145148
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
146149
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
147150
github.com/dvyukov/go-fuzz v0.0.0-20210103155950-6a8e9d1f2415/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
@@ -543,11 +546,13 @@ github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB
543546
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
544547
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
545548
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
549+
github.com/onsi/ginkgo v1.10.2/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
546550
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
547551
github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
548552
github.com/onsi/ginkgo v1.14.1/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
549553
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
550554
github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
555+
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
551556
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
552557
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
553558
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
@@ -556,12 +561,17 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
556561
github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
557562
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
558563
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
564+
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
559565
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
560566
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
561567
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
562568
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
563569
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
564570
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
571+
github.com/pb33f/libopenapi v0.8.1 h1:kudGljKp7cErupRr8ND5oIaTsfjOfQYAxSd3Z2dxt6k=
572+
github.com/pb33f/libopenapi v0.8.1/go.mod h1:lvUmCtjgHUGVj6WzN3I5/CS9wkXtyN3Ykjh6ZZP5lrI=
573+
github.com/pb33f/libopenapi-validator v0.0.7 h1:0ZtV2V28Fu69wIMccNeFHyusmubfoq4zy2uGqrjDHMk=
574+
github.com/pb33f/libopenapi-validator v0.0.7/go.mod h1:uAF035zrQxpAdaoZuZZyy7eh7+Fjw3ovv4bOAPjt97U=
565575
github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU=
566576
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
567577
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
@@ -607,6 +617,8 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
607617
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
608618
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
609619
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
620+
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0 h1:WCcC4vZDS1tYNxjWlwRJZQy28r8CMoggKnxNzxsVDMQ=
621+
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0/go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0=
610622
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
611623
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
612624
github.com/secure-systems-lab/go-securesystemslib v0.3.1/go.mod h1:o8hhjkbNl2gOamKUA/eNW3xUrntHT9L4W89W1nfj43U=
@@ -691,6 +703,8 @@ github.com/vmihailenco/msgpack/v5 v5.3.4/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q
691703
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
692704
github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
693705
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
706+
github.com/vmware-labs/yaml-jsonpath v0.3.2 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk=
707+
github.com/vmware-labs/yaml-jsonpath v0.3.2/go.mod h1:U6whw1z03QyqgWdgXxvVnQ90zN1BWz5V+51Ewf8k+rQ=
694708
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
695709
github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs=
696710
github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM=
@@ -867,6 +881,8 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ
867881
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
868882
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
869883
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
884+
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
885+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
870886
golang.org/x/sys v0.0.0-20170830134202-bb24a47a89ea/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
871887
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
872888
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -1134,6 +1150,7 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
11341150
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
11351151
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
11361152
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
1153+
gopkg.in/yaml.v3 v3.0.0-20191026110619-0b21df46bc1d/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
11371154
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
11381155
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
11391156
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

middleware.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ func Middleware(next http.Handler) http.Handler {
2424
func (s *Speakeasy) Middleware(next http.Handler) http.Handler {
2525
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2626
s.handleRequestResponse(w, r, next.ServeHTTP, func(r *http.Request) string {
27-
var pathHint string
27+
// First check if it matches any OpenAPI document provided
28+
pathHint := s.MatchOpenAPIPath(r)
29+
if pathHint != "" {
30+
return pathHint
31+
}
2832

2933
// First check gorilla/mux for a path hint
3034
route := mux.CurrentRoute(r)
3135
if route != nil {
32-
pathHint, _ = route.GetPathTemplate()
36+
pathHint, _ := route.GetPathTemplate()
3337
if pathHint != "" {
3438
return pathHint
3539
}
@@ -38,7 +42,7 @@ func (s *Speakeasy) Middleware(next http.Handler) http.Handler {
3842
// Check chi router for a path hint
3943
routeContext := chi.RouteContext(r.Context())
4044
if routeContext != nil {
41-
pathHint = routeContext.RoutePattern()
45+
pathHint := routeContext.RoutePattern()
4246
if pathHint != "" {
4347
return pathHint
4448
}
@@ -68,7 +72,10 @@ func MiddlewareWithMux(mux Mux, next http.Handler) http.Handler {
6872
func (s *Speakeasy) MiddlewareWithMux(mux Mux, next http.Handler) http.Handler {
6973
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7074
s.handleRequestResponse(w, r, next.ServeHTTP, func(r *http.Request) string {
71-
var pathHint string
75+
pathHint := s.MatchOpenAPIPath(r)
76+
if pathHint != "" {
77+
return pathHint
78+
}
7279

7380
_, pathHint = mux.Handler(r)
7481

@@ -91,6 +98,11 @@ func (s *Speakeasy) GinMiddleware(c *gin.Context) {
9198
c.Next()
9299
}, func(c *gin.Context) func(r *http.Request) string {
93100
return func(r *http.Request) string {
101+
pathHint := s.MatchOpenAPIPath(r)
102+
if pathHint != "" {
103+
return pathHint
104+
}
105+
94106
return c.FullPath()
95107
}
96108
}(c))
@@ -110,6 +122,11 @@ func (s *Speakeasy) EchoMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
110122

111123
return next(c)
112124
}, func(r *http.Request) string {
125+
pathHint := s.MatchOpenAPIPath(r)
126+
if pathHint != "" {
127+
return pathHint
128+
}
129+
113130
return c.Path()
114131
})
115132
}

middleware_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,105 @@ func TestSpeakeasy_Middleware_DataDogHttpTraceServerMux_PathHint_Success(t *test
12301230
}
12311231
}
12321232

1233+
func TestSpeakeasy_Middleware_Capture_OpenAPIPathHint_Success(t *testing.T) {
1234+
type args struct {
1235+
Method string
1236+
URL string
1237+
Doc string
1238+
}
1239+
tests := []struct {
1240+
Name string
1241+
Args args
1242+
WantPathHint string
1243+
}{
1244+
{
1245+
Name: "captures simple path hint from OpenAPI",
1246+
Args: args{
1247+
Method: http.MethodGet,
1248+
URL: "http://test.com/user",
1249+
Doc: `openapi: 3.0.0
1250+
paths:
1251+
/user:
1252+
get:
1253+
responses:
1254+
'200':
1255+
description: OK`,
1256+
},
1257+
WantPathHint: "/user",
1258+
},
1259+
{
1260+
Name: "captures more complex path hint from OpenAPI",
1261+
Args: args{
1262+
Method: http.MethodGet,
1263+
URL: "http://test.com/user/1/send",
1264+
Doc: `openapi: 3.0.0
1265+
paths:
1266+
/user/{id}/{action}:
1267+
get:
1268+
responses:
1269+
'200':
1270+
description: OK`,
1271+
},
1272+
WantPathHint: "/user/{id}/{action}",
1273+
},
1274+
}
1275+
for _, tt := range tests {
1276+
t.Run(tt.Name, func(t *testing.T) {
1277+
speakeasy.ExportSetMaxCaptureSize(9437184)
1278+
1279+
captured := false
1280+
handled := false
1281+
1282+
speakeasy.ExportSetTimeNow(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
1283+
speakeasy.ExportSetTimeSince(1 * time.Millisecond)
1284+
1285+
wg := &sync.WaitGroup{}
1286+
wg.Add(1)
1287+
1288+
sdkInstance := speakeasy.New(speakeasy.Config{
1289+
APIKey: testAPIKey,
1290+
ApiID: testApiID,
1291+
VersionID: testVersionID,
1292+
GRPCDialer: dialer(func(ctx context.Context, req *ingest.IngestRequest) {
1293+
md, ok := metadata.FromIncomingContext(ctx)
1294+
assert.True(t, ok)
1295+
1296+
apiKeys := md.Get("x-api-key")
1297+
assert.Contains(t, apiKeys, testAPIKey)
1298+
1299+
assert.Equal(t, testApiID, req.ApiId)
1300+
assert.Equal(t, testVersionID, req.VersionId)
1301+
assert.Equal(t, tt.WantPathHint, req.PathHint)
1302+
captured = true
1303+
wg.Done()
1304+
}),
1305+
OpenAPIDocument: []byte(tt.Args.Doc),
1306+
})
1307+
1308+
h := sdkInstance.Middleware(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
1309+
w.WriteHeader(http.StatusOK)
1310+
handled = true
1311+
}))
1312+
1313+
w := httptest.NewRecorder()
1314+
1315+
var req *http.Request
1316+
var err error
1317+
req, err = http.NewRequest(tt.Args.Method, tt.Args.URL, nil)
1318+
assert.NoError(t, err)
1319+
1320+
h.ServeHTTP(w, req)
1321+
1322+
wg.Wait()
1323+
1324+
assert.True(t, handled, "middleware did not call handler")
1325+
assert.True(t, captured, "middleware did not capture request")
1326+
1327+
assert.Equal(t, http.StatusOK, w.Code)
1328+
})
1329+
}
1330+
}
1331+
12331332
func dialer(handlerFunc func(ctx context.Context, req *ingest.IngestRequest)) func() func(context.Context, string) (net.Conn, error) {
12341333
return func() func(context.Context, string) (net.Conn, error) {
12351334
listener := bufconn.Listen(1024 * 1024)

0 commit comments

Comments
 (0)