-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_test.go
56 lines (49 loc) · 1.11 KB
/
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
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"io"
"log"
"net/http"
"strings"
"testing"
)
func TestPost(t *testing.T) {
go func() {
// Returns the same thing posted to it
http.HandleFunc("/post", func(w http.ResponseWriter, r *http.Request) {
// Check if the request method is POST
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Convert the body to a string
buf := new(strings.Builder)
_, err := io.Copy(buf, r.Body)
if err != nil {
log.Fatal(err)
}
_, err = fmt.Fprintf(w, buf.String())
if err != nil {
log.Fatal(err)
}
})
err := http.ListenAndServe(":8000", nil)
if err != nil {
log.Fatal(err)
}
}()
testFlags := Flags{
Url: "http://localhost:8000/post",
PostBody: `{"string":"Testing String","int":69420,"bool":true}`,
Headers: nil,
}
res, err := Post(&testFlags)
if err != nil {
t.Fatal(err)
} else {
resStr := string(res)
if resStr[strings.Index(resStr, "\r\n\r\n")+4:] != `{"string":"Testing String","int":69420,"bool":true}` {
t.Fatal("Server resposne != POST body")
}
}
}