This repository has been archived by the owner on Aug 15, 2020. It is now read-only.
/
web_test.go
144 lines (128 loc) · 4.71 KB
/
web_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Copyright 2012 Marc-Antoine Ruel. Licensed under the Apache License, Version
2.0 (the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or
agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and
limitations under the License. */
package main
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
"github.com/maruel/subcommands"
"github.com/maruel/subcommands/subcommandstest"
"github.com/maruel/ut"
)
// Starts the web server in a separate threads and looks for expected results.
type WebDumbcasAppMock struct {
*DumbcasAppMock
socket net.Listener
closed chan bool
baseURL string
}
func makeWebDumbcasAppMock(t *testing.T) *WebDumbcasAppMock {
return &WebDumbcasAppMock{
DumbcasAppMock: makeDumbcasAppMock(t),
closed: make(chan bool),
}
}
func (f *WebDumbcasAppMock) goWeb() {
ut.AssertEqual(f, nil, f.socket)
cmd := subcommands.FindCommand(f, "web")
r := cmd.CommandRun().(*webRun)
r.Root = "\\foo"
// Simulate -local. It is important to use it while testing otherwise it
// may trigger the Windows firewall.
r.local = true
c := make(chan net.Listener)
go func() {
err := r.main(f, c)
f.GetLog().Printf("Closed: %s", err)
f.closed <- true
}()
f.GetLog().Print("Starting")
f.socket = <-c
f.baseURL = fmt.Sprintf("http://%s", f.socket.Addr().String())
f.GetLog().Printf("Started at %s", f.baseURL)
}
func (f *WebDumbcasAppMock) closeWeb() {
f.socket.Close()
f.socket = nil
f.baseURL = ""
<-f.closed
f.CheckBuffer(false, false)
}
func (f *WebDumbcasAppMock) get(url string, expectedURL string) *http.Response {
r, err := http.Get(f.baseURL + url)
ut.AssertEqual(f, nil, err)
ut.AssertEqualf(f, true, expectedURL == "" || r.Request.URL.Path == expectedURL, "%s != %s", expectedURL, r.Request.URL.Path)
return r
}
func (f *WebDumbcasAppMock) get404(url string) {
r, err := http.Get(f.baseURL + url)
ut.AssertEqual(f, nil, err)
ut.AssertEqual(f, 404, r.StatusCode)
}
func readBody(t *subcommandstest.TB, r *http.Response) string {
bytes, err := ioutil.ReadAll(r.Body)
ut.AssertEqual(t, nil, err)
r.Body.Close()
return string(bytes)
}
func expectedBody(t *subcommandstest.TB, r *http.Response, expected string) {
actual := readBody(t, r)
ut.AssertEqual(t, expected, actual)
}
func TestWeb(t *testing.T) {
t.Parallel()
f := makeWebDumbcasAppMock(t)
cmd := subcommands.FindCommand(f, "web")
ut.AssertEqual(t, true, cmd != nil)
run := cmd.CommandRun().(*webRun)
// Sets -root to an invalid non-empty string.
run.Root = "\\test_web"
// Create a tree of stuff. Call the factory functions directly because we
// can't use Run(). The reason Run() can't be used is because we need the
// channel to get the socket address back.
_, _ = f.DumbcasAppMock.MakeCasTable("")
_, _ = f.DumbcasAppMock.LoadNodesTable("", f.cas)
tree1 := map[string]string{
"file1": "content1",
"dir1/dir2/file2": "content2",
}
sha1tree, nodeName, sha1 := archiveData(f.TB, f.cas, f.nodes, tree1)
nodeName = strings.Replace(nodeName, string(filepath.Separator), "/", -1)
f.GetLog().Print("T: Serve over web and verify files are accessible.")
f.goWeb()
defer f.closeWeb()
f.GetLog().Print("T: Make sure it gets a redirect.", sha1, nodeName)
r := f.get("/content/retrieve/nodes", "/content/retrieve/nodes/")
month := time.Now().UTC().Format("2006-01")
expected := fmt.Sprintf("<html><body><pre><a href=\"%s/\">%s/</a>\n<a href=\"tags/\">tags/</a>\n</pre></body></html>", month, month)
expectedBody(f.TB, r, expected)
f.GetLog().Print("T: Get the directory.")
r = f.get("/content/retrieve/nodes/"+month, "/content/retrieve/nodes/"+month+"/")
actual := readBody(f.TB, r)
re := regexp.MustCompile("\\\"(.*)\\\"")
nodeItems := re.FindStringSubmatch(actual)
ut.AssertEqual(t, 2, len(nodeItems))
ut.AssertEqual(t, nodeName, month+"/"+nodeItems[1])
f.GetLog().Print("T: Get the node.")
r = f.get("/content/retrieve/nodes/"+nodeName, "/content/retrieve/nodes/"+nodeName+"/")
expected = "<html><body><pre><a href=\"dir1/\">dir1/</a>\n<a href=\"file1\">file1</a>\n</pre></body></html>"
expectedBody(f.TB, r, expected)
r = f.get("/content/retrieve/default/"+sha1tree["file1"], "/content/retrieve/default/"+sha1tree["file1"])
expectedBody(f.TB, r, "content1")
r = f.get("/content/retrieve/nodes/"+nodeName+"/file1", "")
expectedBody(f.TB, r, "content1")
r = f.get("/content/retrieve/nodes/"+nodeName+"/dir1/dir2/file2", "")
expectedBody(f.TB, r, "content2")
}