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