-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclientwrap.go
71 lines (56 loc) · 1.87 KB
/
clientwrap.go
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
package Go_Runescape
//Author: Edwin Heerschap
//Defines interfaces, structs and methods used in the package.
import (
"net/http"
"errors"
"io"
"bytes"
)
//IHttpUtil is used for all major function calls in the package.
//An implementing struct should define the Get() method to retrieve the data from the passed url and
//store the information as a *http.Response. See the github repository for more information.
type IHttpClient interface {
Get(url string) (*http.Response, error)
}
/*
ALL Structs and methods from this point downwards are used for testing.
*/
//Is used for testing a failed get request.
//The Get() function returns an error.
type failGetHttpClient struct {
IHttpClient
}
//Get returns an error containing "TEST ERROR"
func (nnhc failGetHttpClient) Get(url string) (*http.Response, error) {
err := errors.New("TEST ERROR")
return &http.Response{}, err
}
//invalidJsonHttpClient is used to test the function responses to invalid json.
type invalidJsonHttpClient struct {
IHttpClient
}
//invalidJsonIOReader is used by the invalidJsonHttpClient to set the
//body of the response from an http request to something invalid.
type invalidJsonIOReader struct {
io.ReadCloser
}
//read returns invalid json in bytes. In fact, its not similar to json at all.
func (iJson invalidJsonIOReader) Read(b []byte) (int, error){
bs := bytes.NewBufferString("Invalid Json Bytes")
bs.Read(b)
return bs.Len(), io.EOF
}
//close is defined to satisfy the io.ReadCloser interface.
func (iJson invalidJsonIOReader) Close() error{
//Doing absolutely nothing (:
return nil
}
//Get returns a *http.Response with invalid json and a nil error.
//Used for testing functions that are trying to parse json from a Get request.
func (ijhc invalidJsonHttpClient) Get(url string) (*http.Response, error) {
resp := http.Response{}
ij := invalidJsonIOReader{}
resp.Body = ij
return &resp, nil
}