Skip to content

Commit 68e5cd2

Browse files
committed
fix: contextualize everything
1 parent 77cd0cd commit 68e5cd2

File tree

5 files changed

+9
-30
lines changed

5 files changed

+9
-30
lines changed

extension_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package jsonschema_test
66

77
import (
8-
"context"
98
"encoding/json"
109
"fmt"
1110
"strconv"
@@ -15,8 +14,6 @@ import (
1514
"github.com/ory/jsonschema/v3"
1615
)
1716

18-
var ctx = context.Background()
19-
2017
func powerOfExt() jsonschema.Extension {
2118
meta, err := jsonschema.CompileString(ctx, "powerOf.json", `{
2219
"properties" : {

go.sum

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWX
3333
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
3434
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
3535
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
36-
github.com/ajg/form v0.0.0-20160822230020-523a5da1a92f h1:zvClvFQwU++UpIUBGC8YmDlfhUrweEy1R1Fj1gu5iIM=
3736
github.com/ajg/form v0.0.0-20160822230020-523a5da1a92f/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
3837
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
3938
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
@@ -339,7 +338,6 @@ github.com/gobuffalo/helpers v0.5.0/go.mod h1:stpgxJ2C7T99NLyAxGUnYMM2zAtBk5NKQR
339338
github.com/gobuffalo/helpers v0.6.0/go.mod h1:pncVrer7x/KRvnL5aJABLAuT/RhKRR9klL6dkUOhyv8=
340339
github.com/gobuffalo/helpers v0.6.1/go.mod h1:wInbDi0vTJKZBviURTLRMFLE4+nF2uRuuL2fnlYo7w4=
341340
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
342-
github.com/gobuffalo/httptest v1.0.2 h1:LWp2khlgA697h4BIYWW2aRxvB93jMnBrbakQ/r2KLzs=
343341
github.com/gobuffalo/httptest v1.0.2/go.mod h1:7T1IbSrg60ankme0aDLVnEY0h056g9M1/ZvpVThtB7E=
344342
github.com/gobuffalo/licenser v0.0.0-20180924033006-eae28e638a42/go.mod h1:Ubo90Np8gpsSZqNScZZkVXXAo5DGhTb+WYFIjlnog8w=
345343
github.com/gobuffalo/licenser v0.0.0-20181025145548-437d89de4f75/go.mod h1:x3lEpYxkRG/XtGCUNkio+6RZ/dlOvLzTI9M1auIwFcw=
@@ -683,7 +681,6 @@ github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVY
683681
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
684682
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
685683
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
686-
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
687684
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
688685
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
689686
github.com/karrick/godirwalk v1.7.5/go.mod h1:2c9FRhkDxdIbgkOnCEvnSWs71Bhugbl46shStcFDJ34=
@@ -747,7 +744,6 @@ github.com/markbates/deplist v1.1.3/go.mod h1:BF7ioVzAJYEtzQN/os4rt8H8Ti3h0T7EoN
747744
github.com/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc=
748745
github.com/markbates/going v1.0.2/go.mod h1:UWCk3zm0UKefHZ7l8BNqi26UyiEMniznk8naLdTcy6c=
749746
github.com/markbates/grift v1.0.4/go.mod h1:wbmtW74veyx+cgfwFhlnnMWqhoz55rnHR47oMXzsyVs=
750-
github.com/markbates/hmax v1.0.0 h1:yo2N0gBoCnUMKhV/VRLHomT6Y9wUm+oQQENuWJqCdlM=
751747
github.com/markbates/hmax v1.0.0/go.mod h1:cOkR9dktiESxIMu+65oc/r/bdY4bE8zZw3OLhLx0X2c=
752748
github.com/markbates/inflect v1.0.0/go.mod h1:oTeZL2KHA7CUX6X+fovmK9OvIOFuqu0TwdQrZjLTh88=
753749
github.com/markbates/inflect v1.0.1/go.mod h1:uv3UVNBe5qBIfCm8O8Q+DW+S1EopeyINj+Ikhc7rnCk=
@@ -1103,7 +1099,6 @@ github.com/unrolled/secure v0.0.0-20180918153822-f340ee86eb8b/go.mod h1:mnPT77IA
11031099
github.com/unrolled/secure v0.0.0-20181005190816-ff9db2ff917f/go.mod h1:mnPT77IAdsi/kV7+Es7y+pXALeV3h7G6dQF6mNYjcLA=
11041100
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
11051101
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
1106-
github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
11071102
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
11081103
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
11091104
github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=

httploader/httploader.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020

2121
"github.com/hashicorp/go-retryablehttp"
2222

23-
"github.com/ory/x/httpx"
24-
2523
"github.com/ory/jsonschema/v3"
2624
)
2725

@@ -31,7 +29,7 @@ const ContextKey = "github.com/ory/jsonschema/v3/httploader.HTTPClient"
3129
func Load(ctx context.Context, url string) (io.ReadCloser, error) {
3230
var hc *retryablehttp.Client
3331
if v := ctx.Value(ContextKey); v == nil {
34-
hc = httpx.NewResilientClient()
32+
return nil, fmt.Errorf("expected a client to be set for %s but received nil", ContextKey)
3533
} else if c, ok := v.(*retryablehttp.Client); ok {
3634
hc = c
3735
} else {

httploader/httploader_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestHTTPLoader(t *testing.T) {
3131
t.Cleanup(ts.Close)
3232

3333
mr := func(t *testing.T, ctx context.Context) string {
34-
res, err := Load(context.Background(), ts.URL)
34+
res, err := Load(context.WithValue(context.Background(), ContextKey, retryablehttp.NewClient()), ts.URL)
3535
require.NoError(t, err)
3636
defer res.Close()
3737
body, err := ioutil.ReadAll(res)

schema_test.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ package jsonschema_test
77
import (
88
"bytes"
99
"context"
10-
"crypto/tls"
1110
"encoding/json"
1211
"errors"
12+
"github.com/hashicorp/go-retryablehttp"
13+
"github.com/ory/jsonschema/v3/httploader"
1314
"io"
1415
"io/ioutil"
1516
"net/http"
@@ -27,6 +28,8 @@ import (
2728

2829
var draft4, draft6, draft7 []byte
2930

31+
var ctx = context.WithValue(context.Background(), httploader.ContextKey, retryablehttp.NewClient())
32+
3033
func init() {
3134
var err error
3235
draft4, err = ioutil.ReadFile("testdata/draft4.json")
@@ -66,7 +69,6 @@ type testGroup struct {
6669
}
6770

6871
func testFolder(t *testing.T, folder string, draft *jsonschema.Draft) {
69-
ctx := context.Background()
7072
server := &http.Server{Addr: "localhost:1234", Handler: http.FileServer(http.Dir("testdata/remotes"))}
7173
go func() {
7274
if err := server.ListenAndServe(); err != http.ErrServerClosed {
@@ -174,7 +176,6 @@ func testFolder(t *testing.T, folder string, draft *jsonschema.Draft) {
174176
}
175177

176178
func TestInvalidSchema(t *testing.T) {
177-
ctx := context.Background()
178179
t.Run("MustCompile with panic", func(t *testing.T) {
179180
defer func() {
180181
if r := recover(); r == nil {
@@ -242,18 +243,14 @@ func TestInvalidSchema(t *testing.T) {
242243
}
243244

244245
func TestCompileURL(t *testing.T) {
245-
ctx := context.Background()
246-
tr := http.DefaultTransport.(*http.Transport)
247-
if tr.TLSClientConfig == nil {
248-
tr.TLSClientConfig = &tls.Config{}
249-
}
250-
tr.TLSClientConfig.InsecureSkipVerify = true
251-
252246
handler := http.FileServer(http.Dir("testdata"))
253247
httpServer := httptest.NewServer(handler)
254248
defer httpServer.Close()
255249
httpsServer := httptest.NewTLSServer(handler)
256250
defer httpsServer.Close()
251+
c := retryablehttp.NewClient()
252+
c.HTTPClient = httpsServer.Client()
253+
ctx := context.WithValue(context.Background(), httploader.ContextKey, c)
257254

258255
validTests := []struct {
259256
schema, doc string
@@ -301,8 +298,6 @@ func TestCompileURL(t *testing.T) {
301298
}
302299

303300
func TestValidateInterface(t *testing.T) {
304-
ctx := context.Background()
305-
306301
files := []string{
307302
"testdata/draft4/type.json",
308303
"testdata/draft4/minimum.json",
@@ -359,7 +354,6 @@ func TestValidateInterface(t *testing.T) {
359354
}
360355

361356
func TestInvalidJsonTypeError(t *testing.T) {
362-
ctx := context.Background()
363357
compiler := jsonschema.NewCompiler()
364358
err := compiler.AddResource("test.json", strings.NewReader(`{ "type": "string"}`))
365359
if err != nil {
@@ -380,7 +374,6 @@ func TestInvalidJsonTypeError(t *testing.T) {
380374
}
381375

382376
func TestExtractAnnotations(t *testing.T) {
383-
ctx := context.Background()
384377
t.Run("false", func(t *testing.T) {
385378
compiler := jsonschema.NewCompiler()
386379

@@ -467,7 +460,6 @@ func toFileURL(path string) string {
467460

468461
// TestPanic tests https://github.com/ory/jsonschema/issues/18
469462
func TestPanic(t *testing.T) {
470-
ctx := context.Background()
471463
schema_d := `
472464
{
473465
"type": "object",
@@ -505,7 +497,6 @@ func TestPanic(t *testing.T) {
505497
}
506498

507499
func TestNonStringFormat(t *testing.T) {
508-
ctx := context.Background()
509500
jsonschema.Formats["even-number"] = func(v interface{}) bool {
510501
switch v := v.(type) {
511502
case json.Number:
@@ -536,7 +527,6 @@ func TestNonStringFormat(t *testing.T) {
536527
}
537528

538529
func TestCompiler_LoadURL(t *testing.T) {
539-
ctx := context.Background()
540530
const (
541531
base = `{ "type": "string" }`
542532
schema = `{ "allOf": [{ "$ref": "base.json" }, { "maxLength": 3 }] }`
@@ -566,7 +556,6 @@ func TestCompiler_LoadURL(t *testing.T) {
566556
}
567557

568558
func TestSchemaReferencesDrafts(t *testing.T) {
569-
ctx := context.Background()
570559
c := jsonschema.NewCompiler()
571560
file := "testdata/reference_draft.json"
572561
t.Log(filepath.Base(file))

0 commit comments

Comments
 (0)