summaryrefslogtreecommitdiff
path: root/client/httpsclient_test.go
diff options
context:
space:
mode:
authorJohn Cai <jcai@gitlab.com>2020-05-04 10:59:49 -0700
committerJohn Cai <jcai@gitlab.com>2020-05-04 14:19:47 -0700
commit91f45342c4ff29a24c61812d539ac745dbb1570a (patch)
treeaec9280d8c4e1c0d02515f1ca9d46a65182cfb14 /client/httpsclient_test.go
parentf62a4b2fb89754372a346f24659212eb8da13601 (diff)
downloadgitlab-shell-jc-refactor-gitlabnet-client.tar.gz
Move gitlabnet client to client packagejc-refactor-gitlabnet-client
Diffstat (limited to 'client/httpsclient_test.go')
-rw-r--r--client/httpsclient_test.go115
1 files changed, 115 insertions, 0 deletions
diff --git a/client/httpsclient_test.go b/client/httpsclient_test.go
new file mode 100644
index 0000000..6c3ae08
--- /dev/null
+++ b/client/httpsclient_test.go
@@ -0,0 +1,115 @@
+package client
+
+import (
+ "fmt"
+ "io/ioutil"
+ "net/http"
+ "path"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitlab-shell/client/testserver"
+ "gitlab.com/gitlab-org/gitlab-shell/internal/testhelper"
+)
+
+func TestSuccessfulRequests(t *testing.T) {
+ testCases := []struct {
+ desc string
+ caFile, caPath string
+ selfSigned bool
+ }{
+ {
+ desc: "Valid CaFile",
+ caFile: path.Join(testhelper.TestRoot, "certs/valid/server.crt"),
+ },
+ {
+ desc: "Valid CaPath",
+ caPath: path.Join(testhelper.TestRoot, "certs/valid"),
+ },
+ {
+ desc: "Self signed cert option enabled",
+ selfSigned: true,
+ },
+ {
+ desc: "Invalid cert with self signed cert option enabled",
+ caFile: path.Join(testhelper.TestRoot, "certs/valid/server.crt"),
+ selfSigned: true,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.desc, func(t *testing.T) {
+ client, cleanup := setupWithRequests(t, tc.caFile, tc.caPath, tc.selfSigned)
+ defer cleanup()
+
+ response, err := client.Get("/hello")
+ require.NoError(t, err)
+ require.NotNil(t, response)
+
+ defer response.Body.Close()
+
+ responseBody, err := ioutil.ReadAll(response.Body)
+ assert.NoError(t, err)
+ assert.Equal(t, string(responseBody), "Hello")
+ })
+ }
+}
+
+func TestFailedRequests(t *testing.T) {
+ testCases := []struct {
+ desc string
+ caFile string
+ caPath string
+ }{
+ {
+ desc: "Invalid CaFile",
+ caFile: path.Join(testhelper.TestRoot, "certs/invalid/server.crt"),
+ },
+ {
+ desc: "Invalid CaPath",
+ caPath: path.Join(testhelper.TestRoot, "certs/invalid"),
+ },
+ {
+ desc: "Empty config",
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.desc, func(t *testing.T) {
+ client, cleanup := setupWithRequests(t, tc.caFile, tc.caPath, false)
+ defer cleanup()
+
+ _, err := client.Get("/hello")
+ require.Error(t, err)
+
+ assert.Equal(t, err.Error(), "Internal API unreachable")
+ })
+ }
+}
+
+func setupWithRequests(t *testing.T, caFile, caPath string, selfSigned bool) (*GitlabNetClient, func()) {
+ testDirCleanup, err := testhelper.PrepareTestRootDir()
+ require.NoError(t, err)
+ defer testDirCleanup()
+
+ requests := []testserver.TestRequestHandler{
+ {
+ Path: "/api/v4/internal/hello",
+ Handler: func(w http.ResponseWriter, r *http.Request) {
+ require.Equal(t, http.MethodGet, r.Method)
+
+ fmt.Fprint(w, "Hello")
+ },
+ },
+ }
+
+ url, cleanup := testserver.StartHttpsServer(t, requests)
+
+ httpClient := NewHTTPClient(url, caFile, caPath, selfSigned, 1)
+
+ client, err := NewGitlabNetClient("", "", "", httpClient)
+ require.NoError(t, err)
+
+ return client, cleanup
+}