Merge branch 'master' of git.curoverse.com:arvados into 11876-r-sdk
[arvados.git] / services / arv-git-httpd / git_handler_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "net/http"
9         "net/http/httptest"
10         "net/url"
11         "regexp"
12
13         check "gopkg.in/check.v1"
14 )
15
16 var _ = check.Suite(&GitHandlerSuite{})
17
18 type GitHandlerSuite struct{}
19
20 func (s *GitHandlerSuite) TestEnvVars(c *check.C) {
21         theConfig = defaultConfig()
22         theConfig.RepoRoot = "/"
23         theConfig.GitoliteHome = "/test/ghh"
24
25         u, err := url.Parse("git.zzzzz.arvadosapi.com/test")
26         c.Check(err, check.Equals, nil)
27         resp := httptest.NewRecorder()
28         req := &http.Request{
29                 Method:     "GET",
30                 URL:        u,
31                 RemoteAddr: "[::1]:12345",
32         }
33         h := newGitHandler()
34         h.(*gitHandler).Path = "/bin/sh"
35         h.(*gitHandler).Args = []string{"-c", "printf 'Content-Type: text/plain\r\n\r\n'; env"}
36
37         h.ServeHTTP(resp, req)
38
39         c.Check(resp.Code, check.Equals, http.StatusOK)
40         body := resp.Body.String()
41         c.Check(body, check.Matches, `(?ms).*^PATH=.*:/test/ghh/bin$.*`)
42         c.Check(body, check.Matches, `(?ms).*^GITOLITE_HTTP_HOME=/test/ghh$.*`)
43         c.Check(body, check.Matches, `(?ms).*^GL_BYPASS_ACCESS_CHECKS=1$.*`)
44         c.Check(body, check.Matches, `(?ms).*^REMOTE_HOST=::1$.*`)
45         c.Check(body, check.Matches, `(?ms).*^REMOTE_PORT=12345$.*`)
46         c.Check(body, check.Matches, `(?ms).*^SERVER_ADDR=`+regexp.QuoteMeta(theConfig.Listen)+`$.*`)
47 }
48
49 func (s *GitHandlerSuite) TestCGIErrorOnSplitHostPortError(c *check.C) {
50         u, err := url.Parse("git.zzzzz.arvadosapi.com/test")
51         c.Check(err, check.Equals, nil)
52         resp := httptest.NewRecorder()
53         req := &http.Request{
54                 Method:     "GET",
55                 URL:        u,
56                 RemoteAddr: "test.bad.address.missing.port",
57         }
58         h := newGitHandler()
59         h.ServeHTTP(resp, req)
60         c.Check(resp.Code, check.Equals, http.StatusInternalServerError)
61         c.Check(resp.Body.String(), check.Equals, "")
62 }