Skip to content

Commit

Permalink
refactor: simplify assets testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed May 12, 2023
1 parent 70dd84f commit ed81b90
Showing 1 changed file with 37 additions and 44 deletions.
81 changes: 37 additions & 44 deletions gateway/assets/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import (
"html/template"
"net/http"
"net/url"
"os"
"strconv"

"github.com/ipfs/boxo/gateway/assets"
)

const (
directoryTemplateFile = "../directory-index.html"
dagTemplateFile = "../dag-index.html"

testPath = "/ipfs/QmFooBarQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7/a/b/c"
)

Expand Down Expand Up @@ -67,60 +64,56 @@ var dagTestData = assets.DagTemplateData{
CodecHex: "0x129",
}

var funcMap = template.FuncMap{
"iconFromExt": func(name string) string {
return "ipfs-_blank" // place-holder
},
"urlEscape": func(rawUrl string) string {
pathURL := url.URL{Path: rawUrl}
return pathURL.String()
},
}

func runTemplate(w http.ResponseWriter, filename string, data interface{}) {
tpl, err := template.New(filename).Funcs(funcMap).ParseFiles("../" + filename)
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse template file: %s", err), http.StatusInternalServerError)
return
}
err = tpl.Execute(w, data)
if err != nil {
http.Error(w, fmt.Sprintf("failed to execute template: %s", err), http.StatusInternalServerError)
return
}
}

func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/dag":
dagTemplate, err := template.New("dag-index.html").ParseFiles(dagTemplateFile)
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse template file: %s", err), http.StatusInternalServerError)
return
}
err = dagTemplate.Execute(w, &dagTestData)
if err != nil {
http.Error(w, fmt.Sprintf("failed to execute template: %s", err), http.StatusInternalServerError)
return
}
runTemplate(w, "dag-index.html", dagTestData)
case "/directory":
directoryTemplate, err := template.New("directory-index.html").Funcs(template.FuncMap{
"iconFromExt": func(name string) string {
return "ipfs-_blank" // place-holder
},
"urlEscape": func(rawUrl string) string {
pathURL := url.URL{Path: rawUrl}
return pathURL.String()
},
}).ParseFiles(directoryTemplateFile)
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse template file: %s", err), http.StatusInternalServerError)
return
}
err = directoryTemplate.Execute(w, &directoryTestData)
runTemplate(w, "directory-index.html", directoryTestData)
case "/error":
statusCode, err := strconv.Atoi(r.URL.Query().Get("code"))
if err != nil {
http.Error(w, fmt.Sprintf("failed to execute template: %s", err), http.StatusInternalServerError)
return
statusCode = 500
}
runTemplate(w, "error.html", &assets.ErrorTemplateData{
Path: "/ipfs/baguqeerabn4wonmz6icnk7dfckuizcsf4e4igua2ohdboecku225xxmujepa",
StatusCode: statusCode,
StatusText: http.StatusText(statusCode),
Error: "this is just an example error",
})
case "/":
html := `<p>Test paths: <a href="/dag">DAG</a>, <a href="/directory">Directory</a>.`
html := `<p>Test paths: <a href="/dag">DAG</a>, <a href="/directory">Directory</a>, <a href="/error?code=500">Error</a>.`
_, _ = w.Write([]byte(html))
default:
http.Redirect(w, r, "/", http.StatusSeeOther)
}
})

if _, err := os.Stat(directoryTemplateFile); err != nil {
wd, _ := os.Getwd()
fmt.Printf("could not open template file %q, relative to %q: %s\n", directoryTemplateFile, wd, err)
os.Exit(1)
}

if _, err := os.Stat(dagTemplateFile); err != nil {
wd, _ := os.Getwd()
fmt.Printf("could not open template file %q, relative to %q: %s\n", dagTemplateFile, wd, err)
os.Exit(1)
}

fmt.Printf("listening on localhost:3000\n")
fmt.Printf("listening on http://localhost:3000/\n")
_ = http.ListenAndServe("localhost:3000", mux)
}

0 comments on commit ed81b90

Please sign in to comment.