mobilenig.TestWithBaseURL   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nop 1
dl 0
loc 22
rs 9.9
c 0
b 0
f 0
1
package mobilenig
2
3
import (
4
	"net/http"
5
	"net/url"
6
	"testing"
7
8
	"github.com/stretchr/testify/assert"
9
)
10
11
func TestWithHTTPClient(t *testing.T) {
12
	t.Run("httpClient is not set when the httpClient is nil", func(t *testing.T) {
13
		// Arrange
14
		config := defaultClientConfig()
15
16
		// Act
17
		WithHTTPClient(nil).apply(config)
18
19
		// Assert
20
		assert.NotNil(t, config.httpClient)
21
	})
22
23
	t.Run("httpClient is set when the httpClient is not nil", func(t *testing.T) {
24
		// Arrange
25
		config := defaultClientConfig()
26
		newClient := &http.Client{Timeout: 300}
27
28
		// Act
29
		WithHTTPClient(newClient).apply(config)
30
31
		// Assert
32
		assert.NotNil(t, config.httpClient)
33
		assert.Equal(t, newClient.Timeout, config.httpClient.Timeout)
34
	})
35
}
36
37
func TestWithEnvironment(t *testing.T) {
38
	t.Run("environment is set successfully", func(t *testing.T) {
39
		// Arrange
40
		environment := TestEnvironment
41
		config := defaultClientConfig()
42
43
		// Act
44
		WithEnvironment(environment).apply(config)
45
46
		// Assert
47
		assert.NotNil(t, config.environment)
48
		assert.Equal(t, environment.String(), config.environment.String())
49
	})
50
51
	t.Run("environment is not set if it's not equal to LIVE or TEST", func(t *testing.T) {
52
		// Arrange
53
		var environment Environment = "DEV"
54
		config := defaultClientConfig()
55
56
		// Act
57
		WithEnvironment(environment).apply(config)
58
59
		// Assert
60
		assert.NotNil(t, config.environment)
61
		assert.Equal(t, defaultClientConfig().environment.String(), config.environment.String())
62
	})
63
}
64
65
func TestWithUsername(t *testing.T) {
66
	t.Run("username is set successfully", func(t *testing.T) {
67
		// Arrange
68
		config := defaultClientConfig()
69
		username := "username"
70
71
		// Act
72
		WithUsername(username).apply(config)
73
74
		// Assert
75
		assert.Equal(t, username, config.username)
76
	})
77
}
78
79
func TestWithAPIKey(t *testing.T) {
80
	t.Run("apiKey is set successfully", func(t *testing.T) {
81
		// Arrange
82
		config := defaultClientConfig()
83
		apiKey := "apiKey"
84
85
		// Act
86
		WithAPIKey(apiKey).apply(config)
87
88
		// Assert
89
		assert.Equal(t, apiKey, config.apiKey)
90
	})
91
}
92
93
func TestWithBaseURL(t *testing.T) {
94
	t.Run("it sets the apiBaseURL successfully", func(t *testing.T) {
95
		// Arrange
96
		config := defaultClientConfig()
97
		endpoint, _ := url.Parse("https://example.com")
98
99
		// Act
100
		WithBaseURL(endpoint).apply(config)
101
102
		// Assert
103
		assert.Equal(t, endpoint.String(), config.baseURL)
104
	})
105
106
	t.Run("it does not set the apiBaseURL when it is nil", func(t *testing.T) {
107
		// Arrange
108
		config := defaultClientConfig()
109
110
		// Act
111
		WithBaseURL(nil).apply(config)
112
113
		// Assert
114
		assert.Equal(t, apiBaseURL, config.baseURL)
115
	})
116
}
117