aboutsummaryrefslogtreecommitdiffstats
path: root/tests/helper/readers.go
blob: 03313aedaeb619f75690ae9b08b610c545ca6b36 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package helper

import (
    "encoding/json"
    "io"
    "io/ioutil"
    "net/http"
    "os"
    "testing"
)

func readJSON(t *testing.T, reader io.Reader, value interface{}) {
    data, err := ioutil.ReadAll(reader)
    err = json.Unmarshal(data, &value)
    if err != nil {
        t.Error(err)
    }
}

func CreateHttpTests(t *testing.T, uri string, value interface{}) {
    resp, err := http.Get(uri)
    if err != nil {
        t.Error(err)

        return
    }
    defer resp.Body.Close()

    readJSON(t, resp.Body, value)
}

func CreateFileTests(t *testing.T, fn string, value interface{}) {
    file, err := os.Open(fn)
    if err != nil {
        t.Error(err)

        return
    }
    defer file.Close()

    readJSON(t, file, value)
}