-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
50 lines (43 loc) · 1.3 KB
/
api_test.go
File metadata and controls
50 lines (43 loc) · 1.3 KB
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
package api
import (
"bytes"
"encoding/json"
"net/http"
"strings"
"testing"
)
func BenchmarkGetAlbums(b *testing.B) {
req, _ := http.NewRequest("GET", "/albums", nil)
benchmarkRequest(b, req)
}
func BenchmarkGetAlbumByIDExists(b *testing.B) {
req, _ := http.NewRequest("GET", "/albums/1", nil)
benchmarkRequest(b, req)
}
func BenchmarkGetAlbumByIDNotFound(b *testing.B) {
req, _ := http.NewRequest("GET", "/albums/999", nil)
benchmarkRequest(b, req)
}
func BenchmarkPostAlbumsValid(b *testing.B) {
newAlbum := album{
ID: "4",
Title: "Kind of Blue",
Artist: "Miles Davis",
Price: 29.99,
}
albumJSON, _ := json.Marshal(newAlbum)
req, _ := http.NewRequest("POST", "/albums", bytes.NewBuffer(albumJSON))
req.Header.Set("Content-Type", "application/json")
benchmarkRequest(b, req)
}
func BenchmarkPostAlbumsInvalidJSON(b *testing.B) {
invalidJSON := `{"id": "5", "title": "Invalid Album", "artist": "Test Artist", "price": "invalid_price"}`
req, _ := http.NewRequest("POST", "/albums", strings.NewReader(invalidJSON))
req.Header.Set("Content-Type", "application/json")
benchmarkRequest(b, req)
}
func BenchmarkPostAlbumsEmptyBody(b *testing.B) {
req, _ := http.NewRequest("POST", "/albums", strings.NewReader(""))
req.Header.Set("Content-Type", "application/json")
benchmarkRequest(b, req)
}