-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_post_test.go
47 lines (39 loc) · 1.04 KB
/
api_post_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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"sync"
"testing"
)
func TestConcurrentPostRequests(t *testing.T) {
const numRequests = 200
const baseURL = "http://localhost:8080/api/" // Replace with your API base URL
var wg sync.WaitGroup
for i := 1; i <= numRequests; i++ {
wg.Add(1)
go func(key string, value any) {
defer wg.Done()
// Prepare JSON payload
payload, err := json.Marshal(map[string]interface{}{"key": key, "value": value})
if err != nil {
t.Errorf("Error marshalling JSON: %v", err)
return
}
// Send POST request
resp, err := http.Post(baseURL, "application/json", bytes.NewBuffer(payload))
if err != nil {
t.Errorf("POST request failed: %v", err)
return
}
defer resp.Body.Close()
// Check response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Unexpected status code: %d", resp.StatusCode)
}
}(fmt.Sprintf("key%d", i), i) // Use loop variables to create string keys and integer values
}
// Wait for all requests to finish
wg.Wait()
}