Passed
Push — master ( 7a190b...51e7b4 )
by Victor Hugo
01:04 queued 11s
created

devto.teardown   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
package devto
2
3
import (
4
	"context"
5
	"errors"
6
	"fmt"
7
	"io"
8
	"net/http"
9
	"net/http/httptest"
10
	"reflect"
11
	"testing"
12
)
13
14
// ----- Testing utilities -----
15
16
// ---------- testing utilities ----------
17
var (
18
	testMux       *http.ServeMux
19
	testClientPro *Client
20
	testClientPub *Client
21
	testServer    *httptest.Server
22
	testConfigPro *Config
23
	testConfigPub *Config
24
)
25
26
func setup() {
27
	testMux = http.NewServeMux()
28
	testServer = httptest.NewServer(testMux)
29
	testConfigPro, _ = NewConfig(true, "demo-token")
30
	testConfigPub, _ = NewConfig(false, "")
31
	testClientPro, _ = NewClient(nil, testConfigPro, nil, testServer.URL)
32
	testClientPub, _ = NewClient(nil, testConfigPub, nil, testServer.URL)
33
}
34
35
func teardown() {
36
	testServer.Close()
37
}
38
39
func TestNewClient(t *testing.T) {
40
	type args struct {
41
		ctx    context.Context
42
		config *Config
43
		bc     httpClient
44
		bu     string
45
	}
46
47
	c, err := NewConfig(true, "dummy")
48
	if err != nil {
49
		t.Fatal(err)
50
	}
51
52
	tests := []struct {
53
		name    string
54
		wantErr bool
55
		err     error
56
		args    args
57
	}{
58
		{
59
			name:    "client is build successfully",
60
			wantErr: false,
61
			err:     nil,
62
			args: args{
63
				ctx:    nil,
64
				config: c,
65
				bc:     nil,
66
				bu:     "",
67
			},
68
		},
69
		{
70
			name:    "client fails if config is nil",
71
			wantErr: true,
72
			err:     ErrMissingConfig,
73
			args: args{
74
				ctx:    nil,
75
				config: nil,
76
				bc:     nil,
77
				bu:     "",
78
			},
79
		},
80
	}
81
82
	for _, tt := range tests {
83
		t.Run(tt.name, func(t *testing.T) {
84
			got, err := NewClient(tt.args.ctx, tt.args.config, tt.args.bc, tt.args.bu)
85
			if tt.wantErr && err != nil {
86
				if !reflect.DeepEqual(err, tt.err) {
87
					t.Errorf("failed on error expectation, got: %v | want %v", err, tt.err)
88
				}
89
			} else {
90
				if !reflect.DeepEqual(tt.err, nil) || got == nil {
91
					t.Errorf("missmatched expectation, got: %v, want: %v", err, tt.err)
92
				}
93
			}
94
		})
95
	}
96
}
97
98
func TestClient_NewRequest(t *testing.T) {
99
	setup()
100
	defer teardown()
101
	type args struct {
102
		m    string
103
		uri  string
104
		body io.Reader
105
	}
106
	tests := []struct {
107
		name    string
108
		args    args
109
		wantErr bool
110
		err     error
111
	}{
112
		{
113
			"new request is created",
114
			args{
115
				m:    "",
116
				uri:  "",
117
				body: nil,
118
			},
119
			false,
120
			nil,
121
		},
122
		{
123
			"new fails for invalid url",
124
			args{
125
				m:    "",
126
				uri:  " http://localhost",
127
				body: nil,
128
			},
129
			true,
130
			errors.New("parse  http://localhost: first path segment in URL cannot contain colon"),
131
		},
132
		{
133
			"new fails for invalid url",
134
			args{
135
				m:    "\\\\\\\\\\\\\\",
136
				uri:  "",
137
				body: nil,
138
			},
139
			true,
140
			fmt.Errorf("net/http: invalid method %q", "\\\\\\\\\\\\\\"),
141
		},
142
	}
143
144
	for _, tt := range tests {
145
		t.Run(tt.name, func(t *testing.T) {
146
			got, err := testClientPro.NewRequest(tt.args.m, tt.args.uri, tt.args.body)
147
			if tt.wantErr && err != nil {
148
				if !reflect.DeepEqual(err.Error(), tt.err.Error()) {
149
					t.Errorf("failed on error expectation, got: %v | want %v", err, tt.err)
150
				}
151
			} else {
152
				if !reflect.DeepEqual(tt.err, nil) || got == nil {
153
					t.Errorf("missmatched expectation, got: %v, want: %v", err, tt.err)
154
				}
155
			}
156
		})
157
	}
158
}
159