site.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Package testsite serves a deterministic dynamic page for demos and integration tests.
  2. package testsite
  3. import (
  4. "net/http"
  5. )
  6. const HTML = `<!doctype html><meta charset="utf-8"><title>CFBrowser Lab</title>
  7. <h1>Browser integration lab</h1><label>Query <input id="query" placeholder="Query"></label>
  8. <label>Password <input type="password" value="hidden-secret"></label>
  9. <button id="search">Search</button><button id="replace">Replace Search</button>
  10. <label><input id="check" type="checkbox">Enabled</label>
  11. <label>Format <select id="format"><option value="cif">CIF</option><option value="xyz">XYZ</option></select></label>
  12. <p id="result">Ready</p><table aria-label="Results"><tr><th>Material</th><th>Barrier</th></tr><tr><td>La2O3</td><td>1.2</td></tr></table>
  13. <a href="/download" download="sample.cif">Download CIF</a>
  14. <button id="blob">Download blob</button><p id="trusted"></p>
  15. <script>
  16. query.oninput=()=>{document.body.dataset.lastInput=query.value};
  17. function search(e){setTimeout(()=>{result.textContent='Results for '+query.value;document.getElementById('trusted').textContent='trusted='+e.isTrusted},40)}
  18. document.getElementById('search').onclick=search;
  19. document.getElementById('replace').onclick=()=>{let old=document.getElementById('search'),n=old.cloneNode(true);old.replaceWith(n);n.onclick=search};
  20. document.getElementById('blob').onclick=()=>{let a=document.createElement('a');a.download='blob.txt';a.href=URL.createObjectURL(new Blob(['blob-data']));a.click()};
  21. document.cookie='lab=active; SameSite=Lax';
  22. setTimeout(()=>document.body.dataset.dynamic='loaded',25);
  23. </script>`
  24. func Handler() http.Handler {
  25. mux := http.NewServeMux()
  26. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  27. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  28. _, _ = w.Write([]byte(HTML))
  29. })
  30. mux.HandleFunc("/shadow", func(w http.ResponseWriter, r *http.Request) {
  31. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  32. _, _ = w.Write([]byte(`<!doctype html><meta charset="utf-8"><title>Shadow lab</title><div id="host"></div><button id="replace">Replace shadow</button><p id="out">Shadow ready</p>
  33. <script>
  34. const root=document.getElementById('host').attachShadow({mode:'open'});
  35. root.innerHTML='<span id="label">Shadow Query</span><input aria-labelledby="label"><div id="inner"></div><p>Shadow visible text</p>Direct shadow text<table aria-label="Shadow table"><tr><td>La2O3 shadow</td></tr></table>';
  36. const inner=root.getElementById('inner').attachShadow({mode:'open'});
  37. inner.innerHTML='<button>Shadow Search</button>';
  38. function bind(){inner.querySelector('button').onclick=e=>{document.getElementById('out').textContent='Shadow result '+root.querySelector('input').value+' trusted='+e.isTrusted};}
  39. bind();document.getElementById('replace').onclick=()=>{let old=inner.querySelector('button');old.replaceWith(old.cloneNode(true));bind()};
  40. const closed=document.createElement('div');document.body.append(closed);closed.attachShadow({mode:'closed'}).innerHTML='<button>Closed secret button</button>';
  41. </script>`))
  42. })
  43. mux.HandleFunc("/long", func(w http.ResponseWriter, r *http.Request) {
  44. w.Header().Set("Content-Type", "text/html")
  45. height := "2200"
  46. if r.URL.Query().Get("huge") == "1" {
  47. height = "50000"
  48. }
  49. _, _ = w.Write([]byte(`<html><body style="margin:0"><div style="height:` + height + `px;background:linear-gradient(red,blue)">Capture fixture</div></body></html>`))
  50. })
  51. mux.HandleFunc("/download", func(w http.ResponseWriter, r *http.Request) {
  52. if c, e := r.Cookie("lab"); e != nil || c.Value != "active" {
  53. http.Error(w, "missing session", http.StatusForbidden)
  54. return
  55. }
  56. w.Header().Set("Content-Disposition", `attachment; filename="sample.cif"`)
  57. w.Header().Set("Content-Type", "chemical/x-cif")
  58. _, _ = w.Write([]byte("data_La2O3\n_cell_length_a 3.94\n"))
  59. })
  60. mux.HandleFunc("/cookie", func(w http.ResponseWriter, r *http.Request) {
  61. w.Header().Set("Content-Type", "text/html")
  62. value := "empty"
  63. if c, e := r.Cookie("lab"); e == nil {
  64. value = c.Value
  65. }
  66. _, _ = w.Write([]byte("<p>cookie=" + value + "</p>"))
  67. })
  68. return mux
  69. }