aboutsummaryrefslogtreecommitdiff
path: root/lib/http/client_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-01-30 17:17:58 +0700
committerShulhan <ms@kilabit.info>2022-01-30 17:17:58 +0700
commitd5c58233592b7642980790754fba015fe3fb04dc (patch)
tree5c3d5a7abe3024fa6196ecbc15f3c08f2eb97ec7 /lib/http/client_test.go
parentd60135a97783a59dce0a4552ceded1f53172f25c (diff)
downloadpakakeh.go-d5c58233592b7642980790754fba015fe3fb04dc.tar.xz
lib/http: implement method Download() on Client
The Download method get a resource from remote server and write it into DownloadRequest.Output (a io.Writer).
Diffstat (limited to 'lib/http/client_test.go')
-rw-r--r--lib/http/client_test.go93
1 files changed, 93 insertions, 0 deletions
diff --git a/lib/http/client_test.go b/lib/http/client_test.go
new file mode 100644
index 00000000..f9065cef
--- /dev/null
+++ b/lib/http/client_test.go
@@ -0,0 +1,93 @@
+// Copyright 2022, Shulhan <ms@kilabit.info>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package http
+
+import (
+ "bytes"
+ "fmt"
+ "testing"
+
+ "github.com/shuLhan/share/lib/test"
+)
+
+func TestClient_Download(t *testing.T) {
+ var (
+ logp = "Download"
+ out bytes.Buffer
+ err error
+
+ clientOpts = ClientOptions{
+ ServerUrl: fmt.Sprintf("http://%s", testServer.Options.Address),
+ }
+ client = NewClient(&clientOpts)
+ )
+
+ cases := []struct {
+ desc string
+ expError string
+ req DownloadRequest
+ }{{
+ desc: "With nil Output",
+ req: DownloadRequest{
+ ClientRequest: ClientRequest{
+ Path: "/redirect/downloads",
+ },
+ },
+ expError: fmt.Sprintf("%s: %s", logp, ErrClientDownloadNoOutput),
+ }, {
+ desc: "With invalid path",
+ req: DownloadRequest{
+ ClientRequest: ClientRequest{
+ Path: "/redirect/downloads",
+ },
+ Output: &out,
+ },
+ expError: fmt.Sprintf("%s: 404 Not Found", logp),
+ }, {
+ desc: "With redirect",
+ req: DownloadRequest{
+ ClientRequest: ClientRequest{
+ Path: "/redirect/download",
+ },
+ Output: &out,
+ },
+ }, {
+ desc: "With redirect and trailing slash",
+ req: DownloadRequest{
+ ClientRequest: ClientRequest{
+ Path: "/redirect/download/",
+ },
+ Output: &out,
+ },
+ }, {
+ desc: "With direct path",
+ req: DownloadRequest{
+ ClientRequest: ClientRequest{
+ Path: "/download",
+ },
+ Output: &out,
+ },
+ }, {
+ desc: "With direct path and trailing slash",
+ req: DownloadRequest{
+ ClientRequest: ClientRequest{
+ Path: "/download/",
+ },
+ Output: &out,
+ },
+ }}
+
+ for _, c := range cases {
+ out.Reset()
+
+ _, err = client.Download(c.req)
+ if err != nil {
+ test.Assert(t, c.desc+" - error", c.expError, err.Error())
+ continue
+ }
+
+ test.Assert(t, c.desc, testDownloadBody, out.Bytes())
+ }
+}