-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
115 lines (89 loc) · 3.14 KB
/
main_test.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gorilla/mux"
)
type InMemoryRepo struct {
_services []Service
}
func NewInMemoryRepo() ServiceRepository {
services := []Service{}
service := Service{ID: 1, Name: "Locate Us", Description: "Awesomeness is HERE!", Versions: 3}
services = append(services, service)
service = Service{ID: 2, Name: "Contact Us", Description: "How can I find you?!", Versions: 2}
services = append(services, service)
return &InMemoryRepo{_services: services}
}
func (r *InMemoryRepo) Services(ctx context.Context) ([]Service, error) {
_, span := tracer.Start(ctx, "Services")
defer span.End()
return r._services, nil
}
func (r *InMemoryRepo) Service(ctx context.Context, id int) (*Service, error) {
_, span := tracer.Start(ctx, "Service")
defer span.End()
services, err := r.Services(ctx)
if err != nil {
return nil, err
}
for _, service := range services {
if service.ID == id {
return &service, nil
}
}
return nil, nil
}
func TestGetServices(t *testing.T) {
// Set up router and handler
repo := NewInMemoryRepo()
handler := NewServiceHandler(repo)
router := mux.NewRouter()
router.HandleFunc("/services", handler.GetServices).Methods("GET")
// Create a request to simulate a GET request to "/services"
req, err := http.NewRequest("GET", "/services", nil)
if err != nil {
t.Fatal(err)
}
// Create a response recorder to capture the response
rr := httptest.NewRecorder()
// Serve the request
router.ServeHTTP(rr, req)
// Assert that the response was successful (status code 200)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
// Assert the expected response body (if applicable)
expectedBody := `[{"id":1,"name":"Locate Us","description":"Awesomeness is HERE!","versions":3},{"id":2,"name":"Contact Us","description":"How can I find you?!","versions":2}]`
if strings.Trim(rr.Body.String(), "\n") != expectedBody {
t.Errorf("handler returned unexpected body: \ngot %v want %v", rr.Body.String(), expectedBody)
}
}
func TestGetServiceByID(t *testing.T) {
// Set up router and handler
handler := NewServiceHandler(NewInMemoryRepo())
router := mux.NewRouter()
router.HandleFunc("/service/{id}", handler.GetService).Methods("GET")
// Create a request to simulate a GET request to "/service/123"
req, err := http.NewRequest("GET", "/service/1", nil)
if err != nil {
t.Fatal(err)
}
req = mux.SetURLVars(req, map[string]string{"id": "1"}) // Set path variable
// Create a response recorder
rr := httptest.NewRecorder()
// Serve the request
router.ServeHTTP(rr, req)
// Assert that the response was successful
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
// Assert the expected response body (if applicable)
expectedBody := `{"id":1,"name":"Locate Us","description":"Awesomeness is HERE!","versions":3}`
if expectedBody != strings.Trim(rr.Body.String(), "\n") {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expectedBody)
}
}