github.com/v2fly/tools@v0.100.0/godoc/server_test.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package godoc 6 7 import ( 8 "net/http" 9 "net/http/httptest" 10 "net/url" 11 "strings" 12 "testing" 13 "text/template" 14 15 "github.com/v2fly/tools/godoc/vfs/mapfs" 16 ) 17 18 // TestIgnoredGoFiles tests the scenario where a folder has no .go or .c files, 19 // but has an ignored go file. 20 func TestIgnoredGoFiles(t *testing.T) { 21 packagePath := "github.com/package" 22 packageComment := "main is documented in an ignored .go file" 23 24 c := NewCorpus(mapfs.New(map[string]string{ 25 "src/" + packagePath + "/ignored.go": `// +build ignore 26 27 // ` + packageComment + ` 28 package main`})) 29 srv := &handlerServer{ 30 p: &Presentation{ 31 Corpus: c, 32 }, 33 c: c, 34 } 35 pInfo := srv.GetPageInfo("/src/"+packagePath, packagePath, NoFiltering, "linux", "amd64") 36 37 if pInfo.PDoc == nil { 38 t.Error("pInfo.PDoc = nil; want non-nil.") 39 } else { 40 if got, want := pInfo.PDoc.Doc, packageComment+"\n"; got != want { 41 t.Errorf("pInfo.PDoc.Doc = %q; want %q.", got, want) 42 } 43 if got, want := pInfo.PDoc.Name, "main"; got != want { 44 t.Errorf("pInfo.PDoc.Name = %q; want %q.", got, want) 45 } 46 if got, want := pInfo.PDoc.ImportPath, packagePath; got != want { 47 t.Errorf("pInfo.PDoc.ImportPath = %q; want %q.", got, want) 48 } 49 } 50 if pInfo.FSet == nil { 51 t.Error("pInfo.FSet = nil; want non-nil.") 52 } 53 } 54 55 func TestIssue5247(t *testing.T) { 56 const packagePath = "example.com/p" 57 c := NewCorpus(mapfs.New(map[string]string{ 58 "src/" + packagePath + "/p.go": `package p 59 60 //line notgen.go:3 61 // F doc //line 1 should appear 62 // line 2 should appear 63 func F() 64 //line foo.go:100`})) // No newline at end to check corner cases. 65 66 srv := &handlerServer{ 67 p: &Presentation{Corpus: c}, 68 c: c, 69 } 70 pInfo := srv.GetPageInfo("/src/"+packagePath, packagePath, 0, "linux", "amd64") 71 if got, want := pInfo.PDoc.Funcs[0].Doc, "F doc //line 1 should appear\nline 2 should appear\n"; got != want { 72 t.Errorf("pInfo.PDoc.Funcs[0].Doc = %q; want %q", got, want) 73 } 74 } 75 76 func testServeBody(t *testing.T, p *Presentation, path, body string) { 77 t.Helper() 78 r := &http.Request{URL: &url.URL{Path: path}} 79 rw := httptest.NewRecorder() 80 p.ServeFile(rw, r) 81 if rw.Code != 200 || !strings.Contains(rw.Body.String(), body) { 82 t.Fatalf("GET %s: expected 200 w/ %q: got %d w/ body:\n%s", 83 path, body, rw.Code, rw.Body) 84 } 85 } 86 87 func TestRedirectAndMetadata(t *testing.T) { 88 c := NewCorpus(mapfs.New(map[string]string{ 89 "doc/y/index.html": "Hello, y.", 90 "doc/x/index.html": `<!--{ 91 "Path": "/doc/x/" 92 }--> 93 94 Hello, x. 95 `})) 96 c.updateMetadata() 97 p := &Presentation{ 98 Corpus: c, 99 GodocHTML: template.Must(template.New("").Parse(`{{printf "%s" .Body}}`)), 100 } 101 102 // Test that redirect is sent back correctly. 103 // Used to panic. See golang.org/issue/40665. 104 for _, elem := range []string{"x", "y"} { 105 dir := "/doc/" + elem + "/" 106 107 r := &http.Request{URL: &url.URL{Path: dir + "index.html"}} 108 rw := httptest.NewRecorder() 109 p.ServeFile(rw, r) 110 loc := rw.Result().Header.Get("Location") 111 if rw.Code != 301 || loc != dir { 112 t.Errorf("GET %s: expected 301 -> %q, got %d -> %q", r.URL.Path, dir, rw.Code, loc) 113 } 114 115 testServeBody(t, p, dir, "Hello, "+elem) 116 } 117 } 118 119 func TestMarkdown(t *testing.T) { 120 p := &Presentation{ 121 Corpus: NewCorpus(mapfs.New(map[string]string{ 122 "doc/test.md": "**bold**", 123 "doc/test2.md": `{{"*template*"}}`, 124 })), 125 GodocHTML: template.Must(template.New("").Parse(`{{printf "%s" .Body}}`)), 126 } 127 128 testServeBody(t, p, "/doc/test.html", "<strong>bold</strong>") 129 testServeBody(t, p, "/doc/test2.html", "<em>template</em>") 130 }