-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilereader_test.go
More file actions
108 lines (93 loc) · 3.17 KB
/
filereader_test.go
File metadata and controls
108 lines (93 loc) · 3.17 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"net/url"
"os"
"strings"
"testing"
)
type TestFileReaderInfo struct {
URL *url.URL
Error string
}
func NewTestFileReader(urlstring, errstr string) FileReader {
url, err := url.Parse(urlstring)
if err != nil {
panic(err)
}
return &TestFileReaderInfo{
URL: url,
Error: errstr,
}
}
//URLScheme returns the scheme of the URL this FileReader has been created for
func (tfr *TestFileReaderInfo) URLScheme() string {
return tfr.URL.Scheme
}
//URLString returns the full URL this FileReader has been created for
func (tfr *TestFileReaderInfo) URLString() string {
return tfr.URL.String()
}
func (tfr *TestFileReaderInfo) ReadFromWeb() ([]byte, error) {
if tfr.Error == "" {
return []byte(`Read from web content`), nil
}
return nil, fmt.Errorf(tfr.Error)
}
func (tfr *TestFileReaderInfo) ReadFromFile() ([]byte, error) {
if tfr.Error == "" {
return []byte(`Read from file content`), nil
}
return nil, fmt.Errorf(tfr.Error)
}
func TestReadFromURLHttp(t *testing.T) {
ur := &URLReaderInfo{FileReader: NewTestFileReader("http://www.domain.com/somefile.json", "")}
c, err := ur.ReadFromURL()
AssertSuccess(t, err)
AreEqual(t, `Read from web content`, string(c), "Incorrect Swagger deserialised")
}
func TestReadFromURLHttps(t *testing.T) {
ur := &URLReaderInfo{FileReader: NewTestFileReader("https://www.andomain.com/otherfile.json", "")}
c, err := ur.ReadFromURL()
AssertSuccess(t, err)
AreEqual(t, `Read from web content`, string(c), "Incorrect Swagger deserialised")
}
func TestReadFromURLFile(t *testing.T) {
ur := &URLReaderInfo{FileReader: NewTestFileReader("file:///tmp/otherfile.json", "")}
c, err := ur.ReadFromURL()
AssertSuccess(t, err)
AreEqual(t, `Read from file content`, string(c), "Incorrect Swagger deserialised")
}
func TestReadFromUrlFailsWithUnrecognisedScheme(t *testing.T) {
ur := &URLReaderInfo{FileReader: NewTestFileReader("zzz:///tmp/otherfile.json", "")}
_, err := ur.ReadFromURL()
IsTrue(t, err != nil, "Unrecognised URL Scheme did not fail")
}
func TestReadFromUrlFailsFailsWhenError(t *testing.T) {
errstr := "Some error happened"
ur := &URLReaderInfo{FileReader: NewTestFileReader("file:///tmp/otherfile.json", errstr)}
_, err := ur.ReadFromURL()
IsTrue(t, err != nil, "Read from file with error did not fail")
AreEqual(t, errstr, err.Error(), "Incorrect message")
}
func TestNewFileReaderFailsWithInvalidURL(t *testing.T) {
_, err := NewFileReader("!£$%£%^$^&$%^")
IsTrue(t, err != nil, "Did not fail creating a new SwaggerReader when passed an invalid URL")
}
func TestReadFromWeb(t *testing.T) {
sr, err := NewFileReader("https://www.google.com")
AssertSuccess(t, err)
c, err := sr.ReadFromWeb()
AssertSuccess(t, err)
AreEqual(t, "<!doctype html>", string(c[0:15]), "Unable to read google.com")
}
func TestReadFromFile(t *testing.T) {
dir, err := os.Getwd()
filepath := fmt.Sprintf("file:///%s/swagger.json", strings.Replace(dir, "\\", "/", -1))
sr, err := NewFileReader(filepath)
AssertSuccess(t, err)
c, err := sr.ReadFromFile()
i := strings.Index(string(c), "\"swagger\"")
IsTrue(t, i > 0, "Swagger version not found in file")
AreEqual(t, "\"swagger\": \"2.0\"", string(c[i:i+16]), "Unable to read file")
}