Skip to content

Commit

Permalink
fix: contextualize everything
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Jan 20, 2022
1 parent 77cd0cd commit 68e5cd2
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 30 deletions.
3 changes: 0 additions & 3 deletions extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package jsonschema_test

import (
"context"
"encoding/json"
"fmt"
"strconv"
Expand All @@ -15,8 +14,6 @@ import (
"github.com/ory/jsonschema/v3"
)

var ctx = context.Background()

func powerOfExt() jsonschema.Extension {
meta, err := jsonschema.CompileString(ctx, "powerOf.json", `{
"properties" : {
Expand Down
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWX
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/ajg/form v0.0.0-20160822230020-523a5da1a92f h1:zvClvFQwU++UpIUBGC8YmDlfhUrweEy1R1Fj1gu5iIM=
github.com/ajg/form v0.0.0-20160822230020-523a5da1a92f/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down Expand Up @@ -339,7 +338,6 @@ github.com/gobuffalo/helpers v0.5.0/go.mod h1:stpgxJ2C7T99NLyAxGUnYMM2zAtBk5NKQR
github.com/gobuffalo/helpers v0.6.0/go.mod h1:pncVrer7x/KRvnL5aJABLAuT/RhKRR9klL6dkUOhyv8=
github.com/gobuffalo/helpers v0.6.1/go.mod h1:wInbDi0vTJKZBviURTLRMFLE4+nF2uRuuL2fnlYo7w4=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/gobuffalo/httptest v1.0.2 h1:LWp2khlgA697h4BIYWW2aRxvB93jMnBrbakQ/r2KLzs=
github.com/gobuffalo/httptest v1.0.2/go.mod h1:7T1IbSrg60ankme0aDLVnEY0h056g9M1/ZvpVThtB7E=
github.com/gobuffalo/licenser v0.0.0-20180924033006-eae28e638a42/go.mod h1:Ubo90Np8gpsSZqNScZZkVXXAo5DGhTb+WYFIjlnog8w=
github.com/gobuffalo/licenser v0.0.0-20181025145548-437d89de4f75/go.mod h1:x3lEpYxkRG/XtGCUNkio+6RZ/dlOvLzTI9M1auIwFcw=
Expand Down Expand Up @@ -683,7 +681,6 @@ github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVY
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/karrick/godirwalk v1.7.5/go.mod h1:2c9FRhkDxdIbgkOnCEvnSWs71Bhugbl46shStcFDJ34=
Expand Down Expand Up @@ -747,7 +744,6 @@ github.com/markbates/deplist v1.1.3/go.mod h1:BF7ioVzAJYEtzQN/os4rt8H8Ti3h0T7EoN
github.com/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc=
github.com/markbates/going v1.0.2/go.mod h1:UWCk3zm0UKefHZ7l8BNqi26UyiEMniznk8naLdTcy6c=
github.com/markbates/grift v1.0.4/go.mod h1:wbmtW74veyx+cgfwFhlnnMWqhoz55rnHR47oMXzsyVs=
github.com/markbates/hmax v1.0.0 h1:yo2N0gBoCnUMKhV/VRLHomT6Y9wUm+oQQENuWJqCdlM=
github.com/markbates/hmax v1.0.0/go.mod h1:cOkR9dktiESxIMu+65oc/r/bdY4bE8zZw3OLhLx0X2c=
github.com/markbates/inflect v1.0.0/go.mod h1:oTeZL2KHA7CUX6X+fovmK9OvIOFuqu0TwdQrZjLTh88=
github.com/markbates/inflect v1.0.1/go.mod h1:uv3UVNBe5qBIfCm8O8Q+DW+S1EopeyINj+Ikhc7rnCk=
Expand Down Expand Up @@ -1103,7 +1099,6 @@ github.com/unrolled/secure v0.0.0-20180918153822-f340ee86eb8b/go.mod h1:mnPT77IA
github.com/unrolled/secure v0.0.0-20181005190816-ff9db2ff917f/go.mod h1:mnPT77IAdsi/kV7+Es7y+pXALeV3h7G6dQF6mNYjcLA=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=
Expand Down
4 changes: 1 addition & 3 deletions httploader/httploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (

"github.com/hashicorp/go-retryablehttp"

"github.com/ory/x/httpx"

"github.com/ory/jsonschema/v3"
)

Expand All @@ -31,7 +29,7 @@ const ContextKey = "github.com/ory/jsonschema/v3/httploader.HTTPClient"
func Load(ctx context.Context, url string) (io.ReadCloser, error) {
var hc *retryablehttp.Client
if v := ctx.Value(ContextKey); v == nil {
hc = httpx.NewResilientClient()
return nil, fmt.Errorf("expected a client to be set for %s but received nil", ContextKey)
} else if c, ok := v.(*retryablehttp.Client); ok {
hc = c
} else {
Expand Down
2 changes: 1 addition & 1 deletion httploader/httploader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestHTTPLoader(t *testing.T) {
t.Cleanup(ts.Close)

mr := func(t *testing.T, ctx context.Context) string {
res, err := Load(context.Background(), ts.URL)
res, err := Load(context.WithValue(context.Background(), ContextKey, retryablehttp.NewClient()), ts.URL)
require.NoError(t, err)
defer res.Close()
body, err := ioutil.ReadAll(res)
Expand Down
25 changes: 7 additions & 18 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ package jsonschema_test
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"github.com/hashicorp/go-retryablehttp"
"github.com/ory/jsonschema/v3/httploader"
"io"
"io/ioutil"
"net/http"
Expand All @@ -27,6 +28,8 @@ import (

var draft4, draft6, draft7 []byte

var ctx = context.WithValue(context.Background(), httploader.ContextKey, retryablehttp.NewClient())

func init() {
var err error
draft4, err = ioutil.ReadFile("testdata/draft4.json")
Expand Down Expand Up @@ -66,7 +69,6 @@ type testGroup struct {
}

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

func TestInvalidSchema(t *testing.T) {
ctx := context.Background()
t.Run("MustCompile with panic", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
Expand Down Expand Up @@ -242,18 +243,14 @@ func TestInvalidSchema(t *testing.T) {
}

func TestCompileURL(t *testing.T) {
ctx := context.Background()
tr := http.DefaultTransport.(*http.Transport)
if tr.TLSClientConfig == nil {
tr.TLSClientConfig = &tls.Config{}
}
tr.TLSClientConfig.InsecureSkipVerify = true

handler := http.FileServer(http.Dir("testdata"))
httpServer := httptest.NewServer(handler)
defer httpServer.Close()
httpsServer := httptest.NewTLSServer(handler)
defer httpsServer.Close()
c := retryablehttp.NewClient()
c.HTTPClient = httpsServer.Client()
ctx := context.WithValue(context.Background(), httploader.ContextKey, c)

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

func TestValidateInterface(t *testing.T) {
ctx := context.Background()

files := []string{
"testdata/draft4/type.json",
"testdata/draft4/minimum.json",
Expand Down Expand Up @@ -359,7 +354,6 @@ func TestValidateInterface(t *testing.T) {
}

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

func TestExtractAnnotations(t *testing.T) {
ctx := context.Background()
t.Run("false", func(t *testing.T) {
compiler := jsonschema.NewCompiler()

Expand Down Expand Up @@ -467,7 +460,6 @@ func toFileURL(path string) string {

// TestPanic tests https://github.com/ory/jsonschema/issues/18
func TestPanic(t *testing.T) {
ctx := context.Background()
schema_d := `
{
"type": "object",
Expand Down Expand Up @@ -505,7 +497,6 @@ func TestPanic(t *testing.T) {
}

func TestNonStringFormat(t *testing.T) {
ctx := context.Background()
jsonschema.Formats["even-number"] = func(v interface{}) bool {
switch v := v.(type) {
case json.Number:
Expand Down Expand Up @@ -536,7 +527,6 @@ func TestNonStringFormat(t *testing.T) {
}

func TestCompiler_LoadURL(t *testing.T) {
ctx := context.Background()
const (
base = `{ "type": "string" }`
schema = `{ "allOf": [{ "$ref": "base.json" }, { "maxLength": 3 }] }`
Expand Down Expand Up @@ -566,7 +556,6 @@ func TestCompiler_LoadURL(t *testing.T) {
}

func TestSchemaReferencesDrafts(t *testing.T) {
ctx := context.Background()
c := jsonschema.NewCompiler()
file := "testdata/reference_draft.json"
t.Log(filepath.Base(file))
Expand Down

0 comments on commit 68e5cd2

Please sign in to comment.