Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package antch | ||
|
||
import ( | ||
"compress/gzip" | ||
"compress/zlib" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestCompressionHandlerWithGzip(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
msg := []byte("hello world") | ||
w.Header().Set("Content-Encoding", "gzip") | ||
zw := gzip.NewWriter(w) | ||
defer zw.Close() | ||
zw.Write(msg) | ||
})) | ||
defer ts.Close() | ||
testCompressionHandler(t, ts, []byte("hello world")) | ||
} | ||
|
||
func TestCompressionHandlerWithDeflate(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
msg := []byte("hello world") | ||
w.Header().Set("Content-Encoding", "deflate") | ||
zw := zlib.NewWriter(w) | ||
defer zw.Close() | ||
zw.Write(msg) | ||
})) | ||
defer ts.Close() | ||
testCompressionHandler(t, ts, []byte("hello world")) | ||
} | ||
|
||
func testCompressionHandler(t *testing.T, ts *httptest.Server, want []byte) { | ||
handler := compressionHandler(defaultMessageHandler()) | ||
req, _ := http.NewRequest("GET", ts.URL, nil) | ||
resp, err := handler.Send(req) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if v := resp.Header.Get("Content-Encoding"); v != "" { | ||
t.Errorf("Content-Encoding = %s; want empty", v) | ||
} | ||
defer resp.Body.Close() | ||
b, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
t.Fatalf("ReadAll: %v", err) | ||
} | ||
if v := string(b); v != string(want) { | ||
t.Errorf("body = %s; want %s", v, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package antch | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestCookiesHandler(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if cookie, err := r.Cookie("Flavor"); err != nil { | ||
http.SetCookie(w, &http.Cookie{Name: "Flavor", Value: "Chocolate Chip"}) | ||
} else { | ||
cookie.Value = "Oatmeal Raisin" | ||
http.SetCookie(w, cookie) | ||
} | ||
})) | ||
defer ts.Close() | ||
|
||
handler := cookiesHandler(defaultMessageHandler()) | ||
req, _ := http.NewRequest("GET", ts.URL, nil) | ||
|
||
resp, err := handler.Send(req) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// After 1st request. | ||
if len(resp.Cookies()) == 0 { | ||
t.Fatal("no cookies data after 1st request") | ||
} | ||
if g, e := resp.Cookies()[0].String(), "Flavor=Chocolate Chip"; e != g { | ||
t.Errorf("expected %s; got %s after 1st request", e, g) | ||
} | ||
|
||
resp, err = handler.Send(req) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// After 2nd request. | ||
if len(resp.Cookies()) == 0 { | ||
t.Fatal("no cookies data after 2nd request") | ||
} | ||
if g, e := resp.Cookies()[0].String(), "Flavor=Oatmeal Raisin"; e != g { | ||
t.Errorf("expected %s; got %s after 2nd request", e, g) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package antch | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// backMessageHandler returns a HttpMessageHandler that use | ||
// custom http.Client | ||
func backMessageHandler(c *http.Client) HttpMessageHandler { | ||
return HttpMessageHandlerFunc(func(req *http.Request) (*http.Response, error) { | ||
return c.Do(req) | ||
}) | ||
} | ||
|
||
// defaultMessageHandler returns a HttpMessageHandler that use | ||
// http.DefaultClient. | ||
func defaultMessageHandler() HttpMessageHandler { | ||
return backMessageHandler(http.DefaultClient) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package antch | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/http/httputil" | ||
"net/url" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func testProxyHandler(t *testing.T, proxyURL *url.URL) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte("hello world")) | ||
})) | ||
defer ts.Close() | ||
|
||
client := &http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: proxyDialContext, | ||
}, | ||
} | ||
|
||
handler := proxyHandler(http.ProxyURL(proxyURL), backMessageHandler(client)) | ||
req, _ := http.NewRequest("GET", ts.URL, nil) | ||
resp, err := handler.Send(req) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
b, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
t.Fatalf("ReadAll: %v", err) | ||
} | ||
if g, e := string(b), "hello world"; g != e { | ||
t.Error("expected %s; got %s", e, g) | ||
} | ||
} | ||
func TestHTTPProxyHandler(t *testing.T) { | ||
// HTTP proxy server. | ||
proxyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
director := func(req *http.Request) { | ||
req.URL.Host = r.Host | ||
req.URL.Scheme = "http" | ||
} | ||
proxy := &httputil.ReverseProxy{Director: director} | ||
proxy.ServeHTTP(w, r) | ||
})) | ||
defer proxyServer.Close() | ||
|
||
proxyURL, _ := url.Parse(proxyServer.URL) | ||
testProxyHandler(t, proxyURL) | ||
} | ||
|
||
func _TestSOCKS5ProxyHandler(t *testing.T) { | ||
gateway, err := net.Listen("tcp", "127.0.0.1:0") | ||
if err != nil { | ||
t.Fatalf("net.Listen failed: %v", err) | ||
} | ||
defer gateway.Close() | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
// Listening and accepting incomeing connection. | ||
c, err := gateway.Accept() | ||
if err != nil { | ||
t.Errorf("net.Listener.Accept failed: %v", err) | ||
return | ||
} | ||
var ( | ||
b = make([]byte, 32) | ||
n = 3 | ||
) | ||
n, _ = io.ReadFull(c, b[:n]) | ||
|
||
c.Close() | ||
}() | ||
|
||
proxyURL, _ := url.Parse(fmt.Sprintf("socks5://%s", gateway.Addr())) | ||
testProxyHandler(t, proxyURL) | ||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package antch | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
const robotsText = "User-agent: * \nDisallow: /account/\nDisallow: /ping\nAllow: /shopping/$\nUser-agent: Twitterbot\nDisallow: /\nSitemap: http://www.bing.com/dict/sitemap-index.xml" | ||
|
||
func TestRobotstxtHandler(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
switch r.URL.Path { | ||
case "/robots.txt": | ||
w.Header().Set("Content-Type", "text/plain") | ||
w.Write([]byte(robotsText)) | ||
case "/": | ||
w.WriteHeader(http.StatusOK) | ||
default: | ||
w.WriteHeader(http.StatusNotFound) | ||
} | ||
})) | ||
defer ts.Close() | ||
|
||
pathTests := []struct { | ||
path string | ||
ua string | ||
allowed bool | ||
}{ | ||
{"/", "", true}, | ||
{"/shopping/", "", true}, | ||
{"account", "", false}, | ||
{"/", "Twitterbot", false}, | ||
} | ||
|
||
handler := robotstxtHandler(defaultMessageHandler()) | ||
for _, test := range pathTests { | ||
req, err := http.NewRequest("GET", ts.URL+test.path, nil) | ||
if err != nil { | ||
t.Fatalf("NewRequest failed: %v", err) | ||
} | ||
if ua := test.ua; ua != "" { | ||
req.Header.Set("User-Agent", ua) | ||
} | ||
|
||
_, err = handler.Send(req) | ||
if test.allowed && err != nil { | ||
t.Errorf("%s(%s) err = %v; want nil", test.path, test.ua, err) | ||
} else if !test.allowed && err == nil { | ||
t.Errorf("%s(%s) err = %v; want request was deny error", test.path, test.ua, err) | ||
} | ||
} | ||
|
||
} |