aboutsummaryrefslogtreecommitdiff
path: root/client_example_test.go
diff options
context:
space:
mode:
authorGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-12-21 11:29:12 +0200
committerGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-12-21 11:29:12 +0200
commit215f1d2caf40258ab0ce86fd44b202b6636640ba (patch)
tree4603584355f29c2e03d75b5fb57979fc59f0cddd /client_example_test.go
parentPull request #21: updated docs (diff)
downloadfasthttp-215f1d2caf40258ab0ce86fd44b202b6636640ba.tar.gz
fasthttp-215f1d2caf40258ab0ce86fd44b202b6636640ba.tar.bz2
fasthttp-215f1d2caf40258ab0ce86fd44b202b6636640ba.zip
Added an example for HostClient
Diffstat (limited to 'client_example_test.go')
-rw-r--r--client_example_test.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/client_example_test.go b/client_example_test.go
new file mode 100644
index 0000000..9a1ad6a
--- /dev/null
+++ b/client_example_test.go
@@ -0,0 +1,39 @@
+package fasthttp_test
+
+import (
+ "log"
+
+ "github.com/valyala/fasthttp"
+)
+
+func ExampleHostClient() {
+ // Perpare a client, which fetches webpages via HTTP proxy listening
+ // on the localhost:8080.
+ c := &fasthttp.HostClient{
+ Addr: "localhost:8080",
+ }
+
+ // Fetch google page via local proxy.
+ statusCode, body, err := c.Get(nil, "http://google.com/foo/bar")
+ if err != nil {
+ log.Fatalf("Error when loading google page through local proxy: %s", err)
+ }
+ if statusCode != fasthttp.StatusOK {
+ log.Fatalf("Unexpected status code: %d. Expecting %d", statusCode, fasthttp.StatusOK)
+ }
+ useResponseBody(body)
+
+ // Fetch foobar page via local proxy. Reuse body buffer.
+ statusCode, body, err = c.Get(body, "http://foobar.com/google/com")
+ if err != nil {
+ log.Fatalf("Error when loading foobar page through local proxy: %s", err)
+ }
+ if statusCode != fasthttp.StatusOK {
+ log.Fatalf("Unexpected status code: %d. Expecting %d", statusCode, fasthttp.StatusOK)
+ }
+ useResponseBody(body)
+}
+
+func useResponseBody(body []byte) {
+ // Do something with body :)
+}