You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
465 lines
11 KiB
Go
465 lines
11 KiB
Go
5 months ago
|
package starnet
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"net/http/httptest"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestUrlEncodeRaw(t *testing.T) {
|
||
|
input := "hello world!@#$%^&*()_+-=~`"
|
||
|
expected := "hello%20world%21%40%23%24%25%5E%26%2A%28%29_%2B-%3D~%60"
|
||
|
result := UrlEncodeRaw(input)
|
||
|
if result != expected {
|
||
|
t.Errorf("UrlEncodeRaw(%q) = %q; want %q", input, result, expected)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestUrlEncode(t *testing.T) {
|
||
|
input := "hello world!@#$%^&*()_+-=~`"
|
||
|
expected := `hello+world%21%40%23%24%25%5E%26%2A%28%29_%2B-%3D~%60`
|
||
|
result := UrlEncode(input)
|
||
|
if result != expected {
|
||
|
t.Errorf("UrlEncode(%q) = %q; want %q", input, result, expected)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestUrlDecode(t *testing.T) {
|
||
|
input := "hello%20world%21%40%23%24%25%5E%26*%28%29_%2B-%3D~%60"
|
||
|
expected := "hello world!@#$%^&*()_+-=~`"
|
||
|
result, err := UrlDecode(input)
|
||
|
if err != nil {
|
||
|
t.Errorf("UrlDecode(%q) returned error: %v", input, err)
|
||
|
}
|
||
|
if result != expected {
|
||
|
t.Errorf("UrlDecode(%q) = %q; want %q", input, result, expected)
|
||
|
}
|
||
|
|
||
|
// Test for error case
|
||
|
invalidInput := "%zz"
|
||
|
_, err = UrlDecode(invalidInput)
|
||
|
if err == nil {
|
||
|
t.Errorf("UrlDecode(%q) expected error, got nil", invalidInput)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestBuildPostForm_WithValidInput(t *testing.T) {
|
||
|
input := map[string]string{
|
||
|
"key1": "value1",
|
||
|
"key2": "value2",
|
||
|
}
|
||
|
|
||
|
expected := []byte("key1=value1&key2=value2")
|
||
|
|
||
|
result := BuildPostForm(input)
|
||
|
|
||
|
if string(result) != string(expected) {
|
||
|
t.Errorf("BuildPostForm(%v) = %v; want %v", input, result, expected)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestBuildPostForm_WithEmptyInput(t *testing.T) {
|
||
|
input := map[string]string{}
|
||
|
|
||
|
expected := []byte("")
|
||
|
|
||
|
result := BuildPostForm(input)
|
||
|
|
||
|
if string(result) != string(expected) {
|
||
|
t.Errorf("BuildPostForm(%v) = %v; want %v", input, result, expected)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestBuildPostForm_WithNilInput(t *testing.T) {
|
||
|
var input map[string]string
|
||
|
|
||
|
expected := []byte("")
|
||
|
|
||
|
result := BuildPostForm(input)
|
||
|
|
||
|
if string(result) != string(expected) {
|
||
|
t.Errorf("BuildPostForm(%v) = %v; want %v", input, result, expected)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestGetRequest(t *testing.T) {
|
||
|
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||
|
rw.Write([]byte(`OK`))
|
||
|
}))
|
||
|
defer server.Close()
|
||
|
|
||
|
resp, err := Get(server.URL)
|
||
|
if err != nil {
|
||
|
t.Errorf("Unexpected error: %v", err)
|
||
|
}
|
||
|
|
||
|
body := resp.Body().String()
|
||
|
if body != "OK" {
|
||
|
t.Errorf("Expected OK, got %v", body)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPostRequest(t *testing.T) {
|
||
|
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||
|
if req.Method != http.MethodPost {
|
||
|
t.Errorf("Expected 'POST', got %v", req.Method)
|
||
|
}
|
||
|
rw.Write([]byte(`OK`))
|
||
|
}))
|
||
|
defer server.Close()
|
||
|
|
||
|
resp, err := Post(server.URL)
|
||
|
if err != nil {
|
||
|
t.Errorf("Unexpected error: %v", err)
|
||
|
}
|
||
|
|
||
|
body := resp.Body().String()
|
||
|
if body != "OK" {
|
||
|
t.Errorf("Expected OK, got %v", body)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestOptionsRequestWithValidInput(t *testing.T) {
|
||
|
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||
|
if req.Method != http.MethodOptions {
|
||
|
t.Errorf("Expected 'OPTIONS', got %v", req.Method)
|
||
|
}
|
||
|
rw.Write([]byte(`OK`))
|
||
|
}))
|
||
|
defer server.Close()
|
||
|
|
||
|
resp, err := Options(server.URL)
|
||
|
if err != nil {
|
||
|
t.Errorf("Unexpected error: %v", err)
|
||
|
}
|
||
|
|
||
|
body := resp.Body().String()
|
||
|
if body != "OK" {
|
||
|
t.Errorf("Expected OK, got %v", body)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPutRequestWithValidInput(t *testing.T) {
|
||
|
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||
|
if req.Method != http.MethodPut {
|
||
|
t.Errorf("Expected 'PUT', got %v", req.Method)
|
||
|
}
|
||
|
rw.Write([]byte(`OK`))
|
||
|
}))
|
||
|
defer server.Close()
|
||
|
|
||
|
resp, err := Put(server.URL)
|
||
|
if err != nil {
|
||
|
t.Errorf("Unexpected error: %v", err)
|
||
|
}
|
||
|
|
||
|
body := resp.Body().String()
|
||
|
if body != "OK" {
|
||
|
t.Errorf("Expected OK, got %v", body)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestDeleteRequestWithValidInput(t *testing.T) {
|
||
|
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||
|
if req.Method != http.MethodDelete {
|
||
|
t.Errorf("Expected 'DELETE', got %v", req.Method)
|
||
|
}
|
||
|
rw.Write([]byte(`OK`))
|
||
|
}))
|
||
|
defer server.Close()
|
||
|
|
||
|
resp, err := Delete(server.URL)
|
||
|
if err != nil {
|
||
|
t.Errorf("Unexpected error: %v", err)
|
||
|
}
|
||
|
|
||
|
body := resp.Body().String()
|
||
|
if body != "OK" {
|
||
|
t.Errorf("Expected OK, got %v", body)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestHeadRequestWithValidInput(t *testing.T) {
|
||
|
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||
|
if req.Method != http.MethodHead {
|
||
|
t.Errorf("Expected 'HEAD', got %v", req.Method)
|
||
|
}
|
||
|
rw.Write([]byte(`OK`))
|
||
|
}))
|
||
|
defer server.Close()
|
||
|
|
||
|
resp, err := Head(server.URL)
|
||
|
if err != nil {
|
||
|
t.Errorf("Unexpected error: %v", err)
|
||
|
}
|
||
|
|
||
|
body := resp.Body().String()
|
||
|
if body == "OK" {
|
||
|
t.Errorf("Expected , got %v", body)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPatchRequestWithValidInput(t *testing.T) {
|
||
|
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||
|
if req.Method != http.MethodPatch {
|
||
|
t.Errorf("Expected 'PATCH', got %v", req.Method)
|
||
|
}
|
||
|
rw.Write([]byte(`OK`))
|
||
|
}))
|
||
|
defer server.Close()
|
||
|
|
||
|
resp, err := Patch(server.URL)
|
||
|
if err != nil {
|
||
|
t.Errorf("Unexpected error: %v", err)
|
||
|
}
|
||
|
|
||
|
body := resp.Body().String()
|
||
|
if body != "OK" {
|
||
|
t.Errorf("Expected OK, got %v", body)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestTraceRequestWithValidInput(t *testing.T) {
|
||
|
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||
|
if req.Method != http.MethodTrace {
|
||
|
t.Errorf("Expected 'TRACE', got %v", req.Method)
|
||
|
}
|
||
|
rw.Write([]byte(`OK`))
|
||
|
}))
|
||
|
defer server.Close()
|
||
|
|
||
|
resp, err := Trace(server.URL)
|
||
|
if err != nil {
|
||
|
t.Errorf("Unexpected error: %v", err)
|
||
|
}
|
||
|
|
||
|
body := resp.Body().String()
|
||
|
if body != "OK" {
|
||
|
t.Errorf("Expected OK, got %v", body)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestConnectRequestWithValidInput(t *testing.T) {
|
||
|
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||
|
if req.Method != http.MethodConnect {
|
||
|
t.Errorf("Expected 'CONNECT', got %v", req.Method)
|
||
|
}
|
||
|
rw.Write([]byte(`OK`))
|
||
|
}))
|
||
|
defer server.Close()
|
||
|
|
||
|
resp, err := Connect(server.URL)
|
||
|
if err != nil {
|
||
|
t.Errorf("Unexpected error: %v", err)
|
||
|
}
|
||
|
|
||
|
body := resp.Body().String()
|
||
|
if body != "OK" {
|
||
|
t.Errorf("Expected OK, got %v", body)
|
||
|
}
|
||
|
}
|
||
|
func TestMethodReturnsCorrectValue(t *testing.T) {
|
||
|
req := NewReq("https://example.com")
|
||
|
req.SetMethodNoError("GET")
|
||
|
if req.Method() != "GET" {
|
||
|
t.Errorf("Expected 'GET', got %v", req.Method())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestSetMethodHandlesInvalidInput(t *testing.T) {
|
||
|
req := NewReq("https://example.com")
|
||
|
err := req.SetMethod("我是谁")
|
||
|
if err == nil {
|
||
|
t.Errorf("Expected error, got nil")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestSetMethodNoErrorSetsMethodCorrectly(t *testing.T) {
|
||
|
req := NewReq("https://example.com")
|
||
|
req.SetMethodNoError("POST")
|
||
|
if req.Method() != "POST" {
|
||
|
t.Errorf("Expected 'POST', got %v", req.Method())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestSetMethodNoErrorIgnoresInvalidInput(t *testing.T) {
|
||
|
req := NewReq("https://example.com")
|
||
|
req.SetMethodNoError("你是谁")
|
||
|
if req.Method() != "GET" {
|
||
|
t.Errorf("Expected '', got %v", req.Method())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestUriReturnsCorrectValue(t *testing.T) {
|
||
|
req := NewReq("https://example.com")
|
||
|
if req.Uri() != "https://example.com" {
|
||
|
t.Errorf("Expected 'https://example.com', got %v", req.Uri())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestSetUriHandlesValidInput(t *testing.T) {
|
||
|
req := NewReq("https://example.com")
|
||
|
err := req.SetUri("https://newexample.com")
|
||
|
if err != nil {
|
||
|
t.Errorf("Unexpected error: %v", err)
|
||
|
}
|
||
|
if req.Uri() != "https://newexample.com" {
|
||
|
t.Errorf("Expected 'https://newexample.com', got %v", req.Uri())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestSetUriHandlesInvalidInput(t *testing.T) {
|
||
|
req := NewReq("https://example.com")
|
||
|
err := req.SetUri("://invalidurl")
|
||
|
if err == nil {
|
||
|
t.Errorf("Expected error, got nil")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestSetUriNoErrorSetsUriCorrectly(t *testing.T) {
|
||
|
req := NewReq("https://example.com")
|
||
|
req.SetUriNoError("https://newexample.com")
|
||
|
if req.Uri() != "https://newexample.com" {
|
||
|
t.Errorf("Expected 'https://newexample.com', got %v", req.Uri())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestSetUriNoErrorIgnoresInvalidInput(t *testing.T) {
|
||
|
req := NewReq("https://example.com")
|
||
|
req.SetUriNoError("://invalidurl")
|
||
|
if req.Uri() != "https://example.com" {
|
||
|
t.Errorf("Expected 'https://example.com', got %v", req.Uri())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type postmanReply struct {
|
||
|
Args struct {
|
||
|
} `json:"args"`
|
||
|
Form map[string]string `json:"form"`
|
||
|
Headers map[string]string `json:"headers"`
|
||
|
Url string `json:"url"`
|
||
|
}
|
||
|
|
||
|
func TestGet(t *testing.T) {
|
||
|
var reply postmanReply
|
||
|
resp, err := NewReq("https://postman-echo.com/get").
|
||
|
AddHeader("hello", "nononmo").
|
||
|
SetAutoCalcContentLength(true).
|
||
|
Do()
|
||
|
if err != nil {
|
||
|
t.Error(err)
|
||
|
}
|
||
|
err = resp.Body().Unmarshal(&reply)
|
||
|
if err != nil {
|
||
|
t.Error(err)
|
||
|
}
|
||
|
fmt.Println(resp.Body().String())
|
||
|
fmt.Println(reply.Headers)
|
||
|
fmt.Println(resp.Cookies())
|
||
|
}
|
||
|
|
||
|
type testData struct {
|
||
|
name string
|
||
|
args *Request
|
||
|
want func(*Response) error
|
||
|
wantErr bool
|
||
|
}
|
||
|
|
||
|
func headerTestData() []testData {
|
||
|
return []testData{
|
||
|
{
|
||
|
name: "addHeader",
|
||
|
args: NewReq("https://postman-echo.com/get").
|
||
|
AddHeader("b612", "test-data").
|
||
|
AddHeader("b612", "test-header").
|
||
|
AddSimpleCookie("b612", "test-cookie").
|
||
|
SetHeader("User-Agent", "starnet test"),
|
||
|
want: func(resp *Response) error {
|
||
|
//fmt.Println(resp.Body().String())
|
||
|
if resp == nil {
|
||
|
return fmt.Errorf("response is nil")
|
||
|
}
|
||
|
if resp.StatusCode != 200 {
|
||
|
return fmt.Errorf("status code is %d", resp.StatusCode)
|
||
|
}
|
||
|
var reply postmanReply
|
||
|
err := resp.Body().Unmarshal(&reply)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if reply.Headers["b612"] != "test-data, test-header" {
|
||
|
return fmt.Errorf("header not found")
|
||
|
}
|
||
|
if reply.Headers["user-agent"] != "starnet test" {
|
||
|
return fmt.Errorf("user-agent not found")
|
||
|
}
|
||
|
if reply.Headers["cookie"] != "b612=test-cookie" {
|
||
|
return fmt.Errorf("cookie not found")
|
||
|
}
|
||
|
return nil
|
||
|
},
|
||
|
wantErr: false,
|
||
|
},
|
||
|
{
|
||
|
name: "postForm",
|
||
|
args: NewSimpleRequest("https://postman-echo.com/post", "POST").
|
||
|
AddHeader("b612", "test-data").
|
||
|
AddHeader("b612", "test-header").
|
||
|
AddSimpleCookie("b612", "test-cookie").
|
||
|
SetHeader("User-Agent", "starnet test").
|
||
|
//SetHeader("Content-Type", "application/x-www-form-urlencoded").
|
||
|
AddFormData("hello", "world").
|
||
|
AddFormData("hello2", "world2").
|
||
|
SetMethodNoError("POST"),
|
||
|
want: func(resp *Response) error {
|
||
|
//fmt.Println(resp.Body().String())
|
||
|
if resp == nil {
|
||
|
return fmt.Errorf("response is nil")
|
||
|
}
|
||
|
if resp.StatusCode != 200 {
|
||
|
return fmt.Errorf("status code is %d", resp.StatusCode)
|
||
|
}
|
||
|
var reply postmanReply
|
||
|
err := resp.Body().Unmarshal(&reply)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if reply.Headers["b612"] != "test-data, test-header" {
|
||
|
return fmt.Errorf("header not found")
|
||
|
}
|
||
|
if reply.Headers["user-agent"] != "starnet test" {
|
||
|
return fmt.Errorf("user-agent not found")
|
||
|
}
|
||
|
if reply.Headers["cookie"] != "b612=test-cookie" {
|
||
|
return fmt.Errorf("cookie not found")
|
||
|
}
|
||
|
if reply.Form["hello"] != "world" {
|
||
|
return fmt.Errorf("form data not found")
|
||
|
}
|
||
|
if reply.Form["hello2"] != "world2" {
|
||
|
return fmt.Errorf("form data not found")
|
||
|
}
|
||
|
return nil
|
||
|
},
|
||
|
wantErr: false,
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
func TestCurl(t *testing.T) {
|
||
|
for _, tt := range headerTestData() {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
got, err := Curl(tt.args)
|
||
|
if (err != nil) != tt.wantErr {
|
||
|
t.Errorf("Curl() error = %v, wantErr %v", err, tt.wantErr)
|
||
|
return
|
||
|
}
|
||
|
if tt.want != nil {
|
||
|
if err := tt.want(got); err != nil {
|
||
|
t.Errorf("Curl() = %v", err)
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|