• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ory / jsonschema / 4607020398

pending completion
4607020398

Pull #9

github

GitHub
Merge 9d5eb0d8c into b41762f04
Pull Request #9: Context

7 of 7 new or added lines in 2 files covered. (100.0%)

1185 of 1334 relevant lines covered (88.83%)

739.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/httploader/httploader.go
1
// Copyright 2017 Santhosh Kumar Tekuri. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4

5
// Package httploader implements loader.Loader for http/https url.
6
//
7
// The package is typically only imported for the side effect of
8
// registering its Loaders.
9
//
10
// To use httploader, link this package into your program:
11
//
12
//        import _ "github.com/ory/jsonschema/v3/httploader"
13
package httploader
14

15
import (
16
        "context"
17
        "fmt"
18
        "io"
19
        "net/http"
20

21
        "github.com/hashicorp/go-retryablehttp"
22

23
        "github.com/ory/jsonschema/v3"
24
)
25

26
type key string
27

28
const ContextKey key = "github.com/ory/jsonschema/v3/httploader.HTTPClient"
29

30
// Load implements jsonschemav2.Loader
31
func Load(ctx context.Context, url string) (io.ReadCloser, error) {
31✔
32
        var hc *retryablehttp.Client
31✔
33
        if v := ctx.Value(ContextKey); v == nil {
31✔
34
                return nil, fmt.Errorf("expected a client to be set for %s but received nil", ContextKey)
×
35
        } else if c, ok := v.(*retryablehttp.Client); ok {
61✔
36
                hc = c
30✔
37
        } else {
31✔
38
                return nil, fmt.Errorf("invalid context value for %s expected %T but got: %T", ContextKey, new(retryablehttp.Client), v)
1✔
39
        }
1✔
40

41
        req, err := retryablehttp.NewRequest("GET", url, nil)
30✔
42
        if err != nil {
30✔
43
                return nil, err
×
44
        }
×
45
        req = req.WithContext(ctx)
30✔
46
        resp, err := hc.Do(req)
30✔
47
        if err != nil {
32✔
48
                return nil, err
2✔
49
        }
2✔
50

51
        if resp.StatusCode != http.StatusOK {
30✔
52
                _ = resp.Body.Close()
2✔
53
                return nil, fmt.Errorf("%s returned status code %d", url, resp.StatusCode)
2✔
54
        }
2✔
55

56
        return resp.Body, nil
26✔
57
}
58

59
func init() {
2✔
60
        jsonschema.Loaders["http"] = Load
2✔
61
        jsonschema.Loaders["https"] = Load
2✔
62
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc