21200: <msg here> Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa.knox@curii.com>
[arvados.git] / services / githttpd / git_handler_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package githttpd
6
7 import (
8         "context"
9         "net/http"
10         "net/http/httptest"
11         "net/url"
12         "regexp"
13
14         "git.arvados.org/arvados.git/lib/config"
15         "git.arvados.org/arvados.git/sdk/go/arvados"
16         "git.arvados.org/arvados.git/sdk/go/ctxlog"
17         check "gopkg.in/check.v1"
18 )
19
20 var _ = check.Suite(&GitHandlerSuite{})
21
22 type GitHandlerSuite struct {
23         cluster *arvados.Cluster
24 }
25
26 func (s *GitHandlerSuite) SetUpTest(c *check.C) {
27         cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
28         c.Assert(err, check.Equals, nil)
29         s.cluster, err = cfg.GetCluster("")
30         c.Assert(err, check.Equals, nil)
31
32         s.cluster.Services.GitHTTP.InternalURLs = map[arvados.URL]arvados.ServiceInstance{{Host: "localhost:80"}: {}}
33         s.cluster.Git.GitoliteHome = "/test/ghh"
34         s.cluster.Git.Repositories = "/"
35 }
36
37 func (s *GitHandlerSuite) TestEnvVars(c *check.C) {
38         u, err := url.Parse("git.zzzzz.arvadosapi.com/test")
39         c.Check(err, check.Equals, nil)
40         resp := httptest.NewRecorder()
41         req := &http.Request{
42                 Method:     "GET",
43                 URL:        u,
44                 RemoteAddr: "[::1]:12345",
45         }
46         h := newGitHandler(context.Background(), s.cluster)
47         h.(*gitHandler).Path = "/bin/sh"
48         h.(*gitHandler).Args = []string{"-c", "printf 'Content-Type: text/plain\r\n\r\n'; env"}
49
50         h.ServeHTTP(resp, req)
51
52         c.Check(resp.Code, check.Equals, http.StatusOK)
53         body := resp.Body.String()
54         c.Check(body, check.Matches, `(?ms).*^PATH=.*:/test/ghh/bin$.*`)
55         c.Check(body, check.Matches, `(?ms).*^GITOLITE_HTTP_HOME=/test/ghh$.*`)
56         c.Check(body, check.Matches, `(?ms).*^GL_BYPASS_ACCESS_CHECKS=1$.*`)
57         c.Check(body, check.Matches, `(?ms).*^REMOTE_HOST=::1$.*`)
58         c.Check(body, check.Matches, `(?ms).*^REMOTE_PORT=12345$.*`)
59         c.Check(body, check.Matches, `(?ms).*^SERVER_ADDR=`+regexp.QuoteMeta("localhost:80")+`$.*`)
60 }
61
62 func (s *GitHandlerSuite) TestCGIErrorOnSplitHostPortError(c *check.C) {
63         u, err := url.Parse("git.zzzzz.arvadosapi.com/test")
64         c.Check(err, check.Equals, nil)
65         resp := httptest.NewRecorder()
66         req := &http.Request{
67                 Method:     "GET",
68                 URL:        u,
69                 RemoteAddr: "test.bad.address.missing.port",
70         }
71         h := newGitHandler(context.Background(), s.cluster)
72         h.ServeHTTP(resp, req)
73         c.Check(resp.Code, check.Equals, http.StatusInternalServerError)
74         c.Check(resp.Body.String(), check.Equals, "")
75 }