2022-12-09 13:43:15 +00:00
|
|
|
|
// Package simpleRequest -----------------------------
|
2023-02-08 14:35:27 +00:00
|
|
|
|
// file : simpleRequest_test.go
|
|
|
|
|
// author : JJXu
|
|
|
|
|
// contact : wavingBear@163.com
|
|
|
|
|
// time : 2022/12/9 20:34:52
|
2022-12-09 13:43:15 +00:00
|
|
|
|
// -------------------------------------------
|
|
|
|
|
package simpleRequest
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type api struct {
|
|
|
|
|
Name string `json:"name" form:"name" query:"name"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func httpserver() {
|
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
switch r.Method {
|
|
|
|
|
case "POST":
|
|
|
|
|
var data api
|
|
|
|
|
jsonDecoder := json.NewDecoder(r.Body)
|
|
|
|
|
jsonDecoder.Decode(&data)
|
|
|
|
|
if data.Name == "JJXu" {
|
|
|
|
|
io.WriteString(w, "ok")
|
|
|
|
|
} else {
|
|
|
|
|
io.WriteString(w, "false")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
io.WriteString(w, "false")
|
|
|
|
|
}
|
|
|
|
|
})
|
2024-01-12 05:12:19 +00:00
|
|
|
|
//fmt.Println("http服务启动了")
|
2022-12-09 13:43:15 +00:00
|
|
|
|
http.ListenAndServe(":8989", nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPost_withSet(t *testing.T) {
|
|
|
|
|
go httpserver()
|
|
|
|
|
var r = NewRequest()
|
|
|
|
|
r.Headers().ConentType_json()
|
|
|
|
|
r.Body().Set("name", "JJXu")
|
|
|
|
|
result, err := r.POST("http://localhost:8989/")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err.Error())
|
|
|
|
|
} else {
|
|
|
|
|
t.Log(string(result))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPost_withSets(t *testing.T) {
|
|
|
|
|
go httpserver()
|
|
|
|
|
|
|
|
|
|
var r = NewRequest()
|
|
|
|
|
r.Headers().ConentType_json()
|
2022-12-09 18:06:12 +00:00
|
|
|
|
r.Body().Sets(map[string]any{
|
2022-12-09 13:43:15 +00:00
|
|
|
|
"name": "JJXu",
|
|
|
|
|
})
|
|
|
|
|
result, err := r.POST("http://localhost:8989/")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err.Error())
|
|
|
|
|
} else {
|
|
|
|
|
t.Log(string(result))
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-09 19:18:35 +00:00
|
|
|
|
|
|
|
|
|
func TestPost_withSetModel(t *testing.T) {
|
|
|
|
|
go httpserver()
|
|
|
|
|
|
|
|
|
|
var r = NewRequest()
|
|
|
|
|
r.Headers().ConentType_json()
|
|
|
|
|
var entry = api{
|
|
|
|
|
Name: "JJXu",
|
|
|
|
|
}
|
|
|
|
|
r.Body().SetModel(&entry)
|
|
|
|
|
result, err := r.POST("http://localhost:8989/")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err.Error())
|
|
|
|
|
} else {
|
|
|
|
|
t.Log(string(result))
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-27 05:40:30 +00:00
|
|
|
|
|
|
|
|
|
// url中的query param字符串参数会被r.QueryParams()中的key值覆盖
|
|
|
|
|
func TestQueryUrl2(t *testing.T) {
|
|
|
|
|
go httpserver()
|
|
|
|
|
|
|
|
|
|
var r = NewRequest()
|
|
|
|
|
r.Headers().ConentType_formUrlencoded()
|
|
|
|
|
r.QueryParams().Set("a", "123")
|
|
|
|
|
r.QueryParams().Set("b", "456")
|
|
|
|
|
_, err := r.POST("http://localhost:8989?a=1&b=2&c=3")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err.Error())
|
|
|
|
|
} else {
|
|
|
|
|
if r.Request.URL.RawQuery != "a=123&b=456&c=3" {
|
2024-01-12 05:12:19 +00:00
|
|
|
|
t.Errorf("query params want '%s' but get '%s'", "a=123&b=456&c=3", r.Request.URL.RawQuery)
|
2023-07-27 05:40:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-01-12 05:12:19 +00:00
|
|
|
|
|
|
|
|
|
// 请求后,r.Request.Body中的内容仍旧可读
|
|
|
|
|
func TestQueryUseStringBody(t *testing.T) {
|
|
|
|
|
go httpserver()
|
|
|
|
|
var r = NewRequest()
|
|
|
|
|
r.Headers().ConentType_json()
|
|
|
|
|
bodyData := "{'a'=123,'b'=56}"
|
|
|
|
|
r.Body().SetString(bodyData)
|
|
|
|
|
_, err := r.POST("http://localhost:8989")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
body, err := io.ReadAll(r.Request.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if string(body) != bodyData {
|
|
|
|
|
t.Errorf("request body want '%s' but get '%s'", bodyData, body)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestEmptyBody(t *testing.T) {
|
|
|
|
|
go httpserver()
|
|
|
|
|
var r = NewRequest()
|
|
|
|
|
r.Headers().ConentType_json()
|
|
|
|
|
_, err := r.POST("http://localhost:8989")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
body, err := io.ReadAll(r.Request.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if string(body) != "{}" {
|
|
|
|
|
t.Errorf("request body want '%s' but get '%s'", "{}", body)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.Headers().ConentType_formUrlencoded()
|
|
|
|
|
_, err = r.POST("http://localhost:8989")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
body, err = io.ReadAll(r.Request.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if string(body) != "" {
|
|
|
|
|
t.Errorf("request body want '%s' but get '%s'", "", body)
|
|
|
|
|
}
|
|
|
|
|
}
|