| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- // Package testsite serves a deterministic dynamic page for demos and integration tests.
- package testsite
- import (
- "net/http"
- )
- const HTML = `<!doctype html><meta charset="utf-8"><title>CFBrowser Lab</title>
- <h1>Browser integration lab</h1><label>Query <input id="query" placeholder="Query"></label>
- <label>Password <input type="password" value="hidden-secret"></label>
- <button id="search">Search</button><button id="replace">Replace Search</button>
- <label><input id="check" type="checkbox">Enabled</label>
- <label>Format <select id="format"><option value="cif">CIF</option><option value="xyz">XYZ</option></select></label>
- <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>
- <a href="/download" download="sample.cif">Download CIF</a>
- <button id="blob">Download blob</button><p id="trusted"></p>
- <script>
- query.oninput=()=>{document.body.dataset.lastInput=query.value};
- function search(e){setTimeout(()=>{result.textContent='Results for '+query.value;document.getElementById('trusted').textContent='trusted='+e.isTrusted},40)}
- document.getElementById('search').onclick=search;
- document.getElementById('replace').onclick=()=>{let old=document.getElementById('search'),n=old.cloneNode(true);old.replaceWith(n);n.onclick=search};
- document.getElementById('blob').onclick=()=>{let a=document.createElement('a');a.download='blob.txt';a.href=URL.createObjectURL(new Blob(['blob-data']));a.click()};
- document.cookie='lab=active; SameSite=Lax';
- setTimeout(()=>document.body.dataset.dynamic='loaded',25);
- </script>`
- func Handler() http.Handler {
- mux := http.NewServeMux()
- mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Content-Type", "text/html; charset=utf-8")
- _, _ = w.Write([]byte(HTML))
- })
- mux.HandleFunc("/shadow", func(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Content-Type", "text/html; charset=utf-8")
- _, _ = 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>
- <script>
- const root=document.getElementById('host').attachShadow({mode:'open'});
- 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>';
- const inner=root.getElementById('inner').attachShadow({mode:'open'});
- inner.innerHTML='<button>Shadow Search</button>';
- function bind(){inner.querySelector('button').onclick=e=>{document.getElementById('out').textContent='Shadow result '+root.querySelector('input').value+' trusted='+e.isTrusted};}
- bind();document.getElementById('replace').onclick=()=>{let old=inner.querySelector('button');old.replaceWith(old.cloneNode(true));bind()};
- const closed=document.createElement('div');document.body.append(closed);closed.attachShadow({mode:'closed'}).innerHTML='<button>Closed secret button</button>';
- </script>`))
- })
- mux.HandleFunc("/long", func(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Content-Type", "text/html")
- height := "2200"
- if r.URL.Query().Get("huge") == "1" {
- height = "50000"
- }
- _, _ = w.Write([]byte(`<html><body style="margin:0"><div style="height:` + height + `px;background:linear-gradient(red,blue)">Capture fixture</div></body></html>`))
- })
- mux.HandleFunc("/download", func(w http.ResponseWriter, r *http.Request) {
- if c, e := r.Cookie("lab"); e != nil || c.Value != "active" {
- http.Error(w, "missing session", http.StatusForbidden)
- return
- }
- w.Header().Set("Content-Disposition", `attachment; filename="sample.cif"`)
- w.Header().Set("Content-Type", "chemical/x-cif")
- _, _ = w.Write([]byte("data_La2O3\n_cell_length_a 3.94\n"))
- })
- mux.HandleFunc("/cookie", func(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Content-Type", "text/html")
- value := "empty"
- if c, e := r.Cookie("lab"); e == nil {
- value = c.Value
- }
- _, _ = w.Write([]byte("<p>cookie=" + value + "</p>"))
- })
- return mux
- }
|