-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
126 lines (115 loc) · 3 KB
/
server_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
116
117
118
119
120
121
122
123
124
125
126
package requests
import (
"compress/gzip"
"fmt"
"io/ioutil"
"net/http"
"os"
"testing"
"time"
)
var port = 8080
func handler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}
func getHandler(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
params := map[string]string{}
for k, v := range query {
params[k] = v[0]
}
body, _ := marshal(params)
w.Write(body)
}
func postHandler(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("content-Type")
if r.Header.Get("Content-Encoding") == "gzip" {
var err error
if r.Body, err = gzip.NewReader(r.Body); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("gzip body: " + err.Error()))
return
}
}
switch contentType {
case "application/x-www-form-urlencoded":
if err := r.ParseForm(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("parse body: " + err.Error()))
return
}
data := map[string]interface{}{}
for k, v := range r.PostForm {
data[k] = v[0]
}
body, _ := marshal(data)
w.Write(body)
return
default:
req, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("invalid body: " + err.Error()))
return
}
w.Write(req)
return
}
}
func timeoutHandler(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second)
w.Write([]byte("OK"))
}
func headerHandler(w http.ResponseWriter, r *http.Request) {
bytes, _ := marshal(r.Header)
w.Write(bytes)
}
func cooclerHandler(w http.ResponseWriter, r *http.Request) {
r.Cookies()
w.Write([]byte("OK"))
}
func uploadFile(w http.ResponseWriter, r *http.Request) {
//fmt.Println("file Upload Endpoint Hit")
// Parse our multipart form, 10 << 20 specifies a maximum
// upload of 10 MB files.
r.ParseMultipartForm(10 << 20)
fileName := r.FormValue("fileField")
if fileName == "" {
fileName = "file"
}
// FormFile returns the first file for the given key `fileName`
// it also returns the FileHeader so we can get the Filename,
// the Header and the size of the file
file, handler, err := r.FormFile(fileName)
if err != nil {
//fmt.Println("Error Retrieving the file", fileName, r.MultipartForm.file, r.MultipartForm.Value)
fmt.Println(err)
return
}
defer file.Close()
fmt.Printf("Uploaded file: %+v\n", handler.Filename)
//fmt.Printf("file Size: %+v\n", handler.Size)
//fmt.Printf("MIME Header: %+v\n", handler.Header)
// read all of the contents of our uploaded file into a
// byte array
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println(err)
}
w.Write(fileBytes)
}
func TestMain(m *testing.M) {
http.HandleFunc("/", handler)
http.HandleFunc("/get", getHandler)
http.HandleFunc("/post", postHandler)
http.HandleFunc("/timeout", timeoutHandler)
http.HandleFunc("/header", headerHandler)
http.HandleFunc("/upload", uploadFile)
go func() {
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
panic(err)
}
}()
code := m.Run()
os.Exit(code)
}