status_service_test.go   A
last analyzed

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 35
dl 0
loc 69
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A client.TestStatusService_Ok 0 15 1
A client.TestBillsService_OkWithError 0 22 1
A client.TestBillsService_OkWithDelay 0 16 1
1
package client
2
3
import (
4
	"context"
5
	"net/http"
6
	"testing"
7
	"time"
8
9
	"github.com/NdoleStudio/go-http-client/internal/helpers"
10
	"github.com/stretchr/testify/assert"
11
)
12
13
func TestStatusService_Ok(t *testing.T) {
14
	// Setup
15
	t.Parallel()
16
17
	// Arrange
18
	client := New()
19
20
	// Act
21
	status, response, err := client.Status.Ok(context.Background())
22
23
	// Assert
24
	assert.Nil(t, err)
25
26
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
27
	assert.Equal(t, &HTTPStatus{Code: 200, Description: "OK"}, status)
28
}
29
30
func TestBillsService_OkWithDelay(t *testing.T) {
31
	// Setup
32
	t.Parallel()
33
	start := time.Now()
34
35
	// Arrange
36
	client := New(WithDelay(500))
37
38
	// Act
39
	status, response, err := client.Status.Ok(context.Background())
40
41
	// Assert
42
	assert.Nil(t, err)
43
	assert.LessOrEqual(t, int64(100), time.Since(start).Milliseconds())
44
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
45
	assert.Equal(t, &HTTPStatus{Code: 200, Description: "OK"}, status)
46
}
47
48
func TestBillsService_OkWithError(t *testing.T) {
49
	// Setup
50
	t.Parallel()
51
52
	// Arrange
53
	server := helpers.MakeTestServer(http.StatusInternalServerError, []byte("Internal Server Error"))
54
	client := New(WithBaseURL(server.URL))
55
56
	// Act
57
	status, response, err := client.Status.Ok(context.Background())
58
59
	// Assert
60
	assert.NotNil(t, err)
61
	assert.Nil(t, status)
62
63
	assert.Equal(t, "500: Internal Server Error, Body: Internal Server Error", err.Error())
64
65
	assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode)
66
	assert.Equal(t, "Internal Server Error", string(*response.Body))
67
68
	// Teardown
69
	server.Close()
70
}
71