Merge branch 'master' into 7492-keepproxy-upstream-errors
[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         u, err := url.Parse("git.zzzzz.arvadosapi.com/test")
19         c.Check(err, check.Equals, nil)
20         resp := httptest.NewRecorder()
21         req := &http.Request{
22                 Method:     "GET",
23                 URL:        u,
24                 RemoteAddr: "[::1]:12345",
25         }
26         h := newGitHandler()
27         h.(*gitHandler).Path = "/bin/sh"
28         h.(*gitHandler).Args = []string{"-c", "printf 'Content-Type: text/plain\r\n\r\n'; env"}
29         os.Setenv("GITOLITE_HTTP_HOME", "/test/ghh")
30         os.Setenv("GL_BYPASS_ACCESS_CHECKS", "yesplease")
31
32         h.ServeHTTP(resp, req)
33
34         c.Check(resp.Code, check.Equals, http.StatusOK)
35         body := resp.Body.String()
36         c.Check(body, check.Matches, `(?ms).*^GITOLITE_HTTP_HOME=/test/ghh$.*`)
37         c.Check(body, check.Matches, `(?ms).*^GL_BYPASS_ACCESS_CHECKS=yesplease$.*`)
38         c.Check(body, check.Matches, `(?ms).*^REMOTE_HOST=::1$.*`)
39         c.Check(body, check.Matches, `(?ms).*^REMOTE_PORT=12345$.*`)
40         c.Check(body, check.Matches, `(?ms).*^SERVER_ADDR=`+regexp.QuoteMeta(theConfig.Addr)+`$.*`)
41 }
42
43 func (s *GitHandlerSuite) TestCGIErrorOnSplitHostPortError(c *check.C) {
44         u, err := url.Parse("git.zzzzz.arvadosapi.com/test")
45         c.Check(err, check.Equals, nil)
46         resp := httptest.NewRecorder()
47         req := &http.Request{
48                 Method:     "GET",
49                 URL:        u,
50                 RemoteAddr: "test.bad.address.missing.port",
51         }
52         h := newGitHandler()
53         h.ServeHTTP(resp, req)
54         c.Check(resp.Code, check.Equals, http.StatusInternalServerError)
55         c.Check(resp.Body.String(), check.Equals, "")
56 }