| 1 |
package goproxy_html_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/elazarl/goproxy" |
| 5 |
"github.com/elazarl/goproxy/ext/html" |
| 6 |
"io/ioutil" |
| 7 |
"net/http" |
| 8 |
"net/http/httptest" |
| 9 |
"net/url" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
type ConstantServer int |
| 14 |
|
| 15 |
func (s ConstantServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 16 |
w.Header().Set("Content-Type", "text/plain; charset=iso-8859-8") |
| 17 |
//w.Header().Set("Content-Type","text/plain; charset=cp-1255") |
| 18 |
w.Write([]byte{0xe3, 0xf3}) |
| 19 |
} |
| 20 |
|
| 21 |
func TestCharset(t *testing.T) { |
| 22 |
s := httptest.NewServer(ConstantServer(1)) |
| 23 |
defer s.Close() |
| 24 |
|
| 25 |
ch := make(chan string, 2) |
| 26 |
proxy := goproxy.NewProxyHttpServer() |
| 27 |
proxy.OnResponse().Do(goproxy_html.HandleString( |
| 28 |
func(s string, ctx *goproxy.ProxyCtx) string { |
| 29 |
ch <- s |
| 30 |
return s |
| 31 |
})) |
| 32 |
proxyServer := httptest.NewServer(proxy) |
| 33 |
defer proxyServer.Close() |
| 34 |
|
| 35 |
proxyUrl, _ := url.Parse(proxyServer.URL) |
| 36 |
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}} |
| 37 |
|
| 38 |
resp, err := client.Get(s.URL + "/cp1255.txt") |
| 39 |
if err != nil { |
| 40 |
t.Fatal("GET:", err) |
| 41 |
} |
| 42 |
b, err := ioutil.ReadAll(resp.Body) |
| 43 |
if err != nil { |
| 44 |
t.Fatal("readAll:", err) |
| 45 |
} |
| 46 |
resp.Body.Close() |
| 47 |
|
| 48 |
inHandleString := "" |
| 49 |
select { |
| 50 |
case inHandleString = <-ch: |
| 51 |
default: |
| 52 |
} |
| 53 |
|
| 54 |
if len(b) != 2 || b[0] != 0xe3 || b[1] != 0xf3 { |
| 55 |
t.Error("Did not translate back to 0xe3,0xf3, instead", b) |
| 56 |
} |
| 57 |
if inHandleString != "דף" { |
| 58 |
t.Error("HandleString did not convert DALET & PEH SOFIT (דף) from ISO-8859-8 to utf-8, got", []byte(inHandleString)) |
| 59 |
} |
| 60 |
} |