Переглянути джерело

Test params in the webapp, not by arguments

Alexandre Dieulot 5 роки тому
батько
коміт
a27f5d0b1c
3 змінених файлів з 89 додано та 19 видалено
  1. 34 18
      test/app.js
  2. 14 1
      test/footer.html
  3. 41 0
      test/header.html

+ 34 - 18
test/app.js

@@ -6,23 +6,36 @@ const crypto = require('crypto')
 const sleep = require('util').promisify(setTimeout)
 
 const argvIndexOfFile = process.argv.indexOf(__filename)
-let DATA_INSTANT = parseInt(process.argv[argvIndexOfFile + 1])
-if (isNaN(DATA_INSTANT)) {
-  DATA_INSTANT = 0
-}
-let SLEEP_TIME = parseInt(process.argv[argvIndexOfFile + 2])
-if (isNaN(SLEEP_TIME)) {
-  SLEEP_TIME = 200
-}
-let CACHE_MAX_AGE = parseInt(process.argv[argvIndexOfFile + 3])
-if (isNaN(CACHE_MAX_AGE)) {
-  CACHE_MAX_AGE = 0
-}
-let PORT = parseInt(process.argv[argvIndexOfFile + 4])
+let PORT = parseInt(process.argv[argvIndexOfFile + 1])
 if (isNaN(PORT)) {
   PORT = 8000
 }
 
+let DATA_INSTANT = 0
+let SLEEP_TIME = 200
+let CACHE_MAX_AGE = 0
+
+function handleCookies(req) {
+  const cookies = req.headers.cookie
+
+  if (!cookies) {
+    return
+  }
+
+  cookies.split('; ').map((cookie) => {
+    const [key, value] = cookie.split('=')
+
+    if (key != 'instantpage_test') {
+      return
+    }
+
+    const cookieValueSplit = value.split(',').map((param) => parseInt(param))
+    DATA_INSTANT = cookieValueSplit[0]
+    SLEEP_TIME = cookieValueSplit[1]
+    CACHE_MAX_AGE = cookieValueSplit[2]
+  })
+}
+
 function sha384(data) {
   const hash = crypto.createHash('sha384')
   hash.update(data)
@@ -50,6 +63,8 @@ async function requestListener(req, res) {
     content += jsContent
   }
   else if (!isNaN(page)) {
+    handleCookies(req)
+
     await sleep(SLEEP_TIME)
 
     if (CACHE_MAX_AGE) {
@@ -58,14 +73,15 @@ async function requestListener(req, res) {
 
     content += await fsPromises.readFile(path.resolve(__dirname, 'header.html'))
 
-    if (DATA_INSTANT) {
-      content += `<body>`
-    }
-    else {
-      content += `<body data-instant-allow-query-string data-instant-allow-external-links >`
+    if (!DATA_INSTANT) {
+      content = content.replace('<body>', '<body data-instant-allow-query-string data-instant-allow-external-links >')
     }
     dataInstantAttribute = DATA_INSTANT ? `data-instant` : ``
 
+    content = content.replace(':checked_aqsael', DATA_INSTANT ? 'checked' : '')
+    content = content.replace(':value_sleep', `value="${SLEEP_TIME}"`)
+    content = content.replace(':value_cacheAge', `value="${CACHE_MAX_AGE}"`)
+
     content += `<h1>Page ${page}</h1>`
     for (let i = 1; i <= 3; i++) {
       if (page != i) {

+ 14 - 1
test/footer.html

@@ -1,6 +1,6 @@
 <button>Test</button>
 
-<script>
+<script type="module">
 let start
 let lastTouchTimestamp = 0
 const button = document.querySelector('button')
@@ -20,4 +20,17 @@ button.addEventListener('click', () => {
   start = undefined
 })
 </script>
+<script type="module">
+const form = document.forms[0]
+form.addEventListener('submit', (e) => {
+  e.preventDefault()
+  const cookieValue = [
+    form.aqsael.checked ? 1 : 0,
+    form.sleep.value,
+    form.cacheAge.value,
+  ].join(',')
+  document.cookie = `instantpage_test=${cookieValue}`
+  location.reload()
+})
+</script>
 <script src="/instantpage.js" type="module" integrity="sha384-__HASH__"></script>

+ 41 - 0
test/header.html

@@ -27,4 +27,45 @@ a:hover {
 a:active {
   background: hsla(0, 0%, 0%, .25);
 }
+
+form {
+  background: hsla(0, 0%, 100%, .25);
+  border: 1px solid hsla(0, 0%, 0%, .15);
+}
+
+form label {
+  display: block;
+  padding: 5px;
+}
+
+@media (min-width: 900px) {
+  form label {
+    display: inline-block;
+    margin-right: 10px;
+  }
+}
+
+form input[type="number"] {
+  width: 50px;
+}
 </style>
+
+<body>
+
+<form>
+  <label>
+    Allow query string &amp; external links:
+    <input name="aqsael" type="checkbox" :checked_aqsael>
+  </label>
+  <label>
+    Sleep time:
+    <input name="sleep" type="number" :value_sleep>
+  </label>
+  <label>
+    Cache max age (in seconds):
+    <input name="cacheAge" type="number" :value_cacheAge>
+  </label>
+  <label>
+    <input type="submit" value="OK">
+  </label>
+</form>