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") }
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) }