Passed
Push — main ( 2798bc...c9aa15 )
by Acho
04:27 queued 02:13
created

ynote.TestWithAPIURL   A

Complexity

Conditions 3

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nop 1
dl 0
loc 29
rs 9.75
c 0
b 0
f 0
1
package ynote
2
3
import (
4
	"net/http"
5
	"testing"
6
7
	"github.com/stretchr/testify/assert"
8
)
9
10
func TestWithHTTPClient(t *testing.T) {
11
	t.Run("httpClient is not set when the httpClient is nil", func(t *testing.T) {
12
		// Setup
13
		t.Parallel()
14
15
		// Arrange
16
		config := defaultClientConfig()
17
18
		// Act
19
		WithHTTPClient(nil).apply(config)
20
21
		// Assert
22
		assert.NotNil(t, config.httpClient)
23
	})
24
25
	t.Run("httpClient is set when the httpClient is not nil", func(t *testing.T) {
26
		// Setup
27
		t.Parallel()
28
29
		// Arrange
30
		config := defaultClientConfig()
31
		newClient := &http.Client{Timeout: 300}
32
33
		// Act
34
		WithHTTPClient(newClient).apply(config)
35
36
		// Assert
37
		assert.NotNil(t, config.httpClient)
38
		assert.Equal(t, newClient.Timeout, config.httpClient.Timeout)
39
	})
40
}
41
42
func TestWithAPIURL(t *testing.T) {
43
	t.Run("apiURL is set successfully", func(t *testing.T) {
44
		// Setup
45
		t.Parallel()
46
47
		// Arrange
48
		apiURL := "https://example.com"
49
		config := defaultClientConfig()
50
51
		// Act
52
		WithAPIURL(apiURL).apply(config)
53
54
		// Assert
55
		assert.Equal(t, apiURL, config.apiURL)
56
	})
57
58
	t.Run("tailing / is trimmed from apiURL", func(t *testing.T) {
59
		// Setup
60
		t.Parallel()
61
62
		// Arrange
63
		apiURL := "https://example.com/"
64
		config := defaultClientConfig()
65
66
		// Act
67
		WithAPIURL(apiURL).apply(config)
68
69
		// Assert
70
		assert.Equal(t, "https://example.com", config.apiURL)
71
	})
72
}
73
74
func TestWithClientID(t *testing.T) {
75
	t.Run("clientID is set successfully", func(t *testing.T) {
76
		// Setup
77
		t.Parallel()
78
79
		// Arrange
80
		username := "client-id"
81
		config := defaultClientConfig()
82
83
		// Act
84
		WithClientID(username).apply(config)
85
86
		// Assert
87
		assert.Equal(t, username, config.clientID)
88
	})
89
}
90
91
func TestWithClientSecret(t *testing.T) {
92
	t.Run("clientSecret is set successfully", func(t *testing.T) {
93
		// Setup
94
		t.Parallel()
95
96
		// Arrange
97
		password := "client-secret"
98
		config := defaultClientConfig()
99
100
		// Act
101
		WithClientSecret(password).apply(config)
102
103
		// Assert
104
		assert.Equal(t, password, config.clientSecret)
105
	})
106
}
107
108
func TestWithCustomerKey(t *testing.T) {
109
	t.Run("customerKey is set successfully", func(t *testing.T) {
110
		// Setup
111
		t.Parallel()
112
113
		// Arrange
114
		customerKey := "key-1"
115
		config := defaultClientConfig()
116
117
		// Act
118
		WithCustomerKey(customerKey).apply(config)
119
120
		// Assert
121
		assert.Equal(t, customerKey, config.customerKey)
122
	})
123
}
124
125
func TestWith(t *testing.T) {
126
	t.Run("customerSecret is set successfully", func(t *testing.T) {
127
		// Setup
128
		t.Parallel()
129
130
		// Arrange
131
		customerSecret := "secret-1"
132
		config := defaultClientConfig()
133
134
		// Act
135
		WithCustomerSecret(customerSecret).apply(config)
136
137
		// Assert
138
		assert.Equal(t, customerSecret, config.customerSecret)
139
	})
140
}
141