73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
|
// Copyright (c) 2022 Nadeen Udantha <me@nadeen.lk>. All rights reserved.
|
||
|
|
||
|
package boihttp_test
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"bytes"
|
||
|
"context"
|
||
|
"net"
|
||
|
"testing"
|
||
|
|
||
|
"git.nadeen.lk/boihttp"
|
||
|
"golang.org/x/net/proxy"
|
||
|
)
|
||
|
|
||
|
const httpbin_addr = "httpbin.org:80"
|
||
|
const httpbin_url = "http://httpbin.org/anything"
|
||
|
|
||
|
func TestClientDoConn(t *testing.T) {
|
||
|
c, err := net.Dial("tcp", httpbin_addr)
|
||
|
throw(err)
|
||
|
defer c.Close()
|
||
|
r, w := bufio.NewReaderSize(c, 1024), bufio.NewWriterSize(c, 1024)
|
||
|
res, body, err := boihttp.DoConn(bufio.NewReadWriter(r, w), "GET", httpbin_url, nil, boihttp.EmptyBody)
|
||
|
throw(err)
|
||
|
print(res, body)
|
||
|
}
|
||
|
|
||
|
func TestClientDoProxy(t *testing.T) {
|
||
|
d, err := proxy.SOCKS5("tcp", "127.0.0.1:1080", nil, proxy.Direct)
|
||
|
throw(err)
|
||
|
res, body, err := boihttp.Do("GET", httpbin_url, nil, boihttp.EmptyBody, d.(proxy.ContextDialer).DialContext, context.Background())
|
||
|
throw(err)
|
||
|
print(res, body)
|
||
|
}
|
||
|
|
||
|
func TestClientDoGet(t *testing.T) {
|
||
|
res, body, err := boihttp.Do("GET", httpbin_url, nil, boihttp.EmptyBody, nil, context.Background())
|
||
|
throw(err)
|
||
|
print(res, body)
|
||
|
}
|
||
|
func TestClientGet(t *testing.T) {
|
||
|
res, body, err := boihttp.Get(httpbin_url, nil, context.Background())
|
||
|
throw(err)
|
||
|
print(res, body)
|
||
|
}
|
||
|
|
||
|
func TestClientDoPost(t *testing.T) {
|
||
|
reqbody := []byte("what's up doc?")
|
||
|
hdrs := make(boihttp.Headers)
|
||
|
hdrs.SetContentLength(len(reqbody))
|
||
|
res, body, err := boihttp.Do("POST", httpbin_url, hdrs, bytes.NewReader(reqbody), nil, context.Background())
|
||
|
throw(err)
|
||
|
print(res, body)
|
||
|
}
|
||
|
|
||
|
func TestClientDoMultiple(t *testing.T) {
|
||
|
c, err := net.Dial("tcp", httpbin_addr)
|
||
|
throw(err)
|
||
|
defer c.Close()
|
||
|
r, w := bufio.NewReaderSize(c, 1024), bufio.NewWriterSize(c, 1024)
|
||
|
hdrs := make(boihttp.Headers)
|
||
|
hdrs.SetKeepAlive(true)
|
||
|
for z := 4; z >= 0; z-- {
|
||
|
if z == 0 {
|
||
|
hdrs.SetKeepAlive(false)
|
||
|
}
|
||
|
res, body, err := boihttp.DoConn(bufio.NewReadWriter(r, w), "GET", httpbin_url, hdrs.Clone(), boihttp.EmptyBody)
|
||
|
throw(err)
|
||
|
print(res, body)
|
||
|
}
|
||
|
}
|