form.es.tst 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. form.tst - Form-based authentication tests
  3. */
  4. const HTTP = tget('TM_HTTP') || "127.0.0.1:8080"
  5. let http: Http = new Http
  6. // Will be denied
  7. http.get(HTTP + "/auth/form/index.html")
  8. ttrue(http.status == 302)
  9. let location = http.header('location')
  10. ttrue(Uri(location).path == '/auth/form/login.html')
  11. // Will return login form
  12. http.get(location)
  13. ttrue(http.status == 200)
  14. ttrue(http.response.contains("<form"))
  15. ttrue(http.response.contains('action="/action/login"'))
  16. // Login. Should succeed with the response being a redirect to /auth/form/index.html
  17. http.reset()
  18. http.form(HTTP + "/action/login", {username: "joshua", password: "pass1"})
  19. ttrue(http.status == 302)
  20. location = http.header('location')
  21. ttrue(Uri(location).path == '/auth/form/index.html')
  22. let cookie = http.header("Set-Cookie")
  23. ttrue(cookie.match(/(-goahead-session-=.*);/)[1])
  24. // Now logged in
  25. http.reset()
  26. http.setCookie(cookie)
  27. http.get(HTTP + "/auth/form/index.html")
  28. ttrue(http.status == 200)
  29. // Now log out. Will be redirected to the login page.
  30. http.reset()
  31. http.setCookie(cookie)
  32. http.post(HTTP + "/action/logout")
  33. ttrue(http.status == 302)
  34. let location = http.header('location')
  35. ttrue(Uri(location).path == '/auth/form/login.html')
  36. // Now should fail to access index.html and get the login page again.
  37. http.get(HTTP + "/auth/form/index.html")
  38. ttrue(http.status == 302)
  39. let location = http.header('location')
  40. ttrue(Uri(location).path == '/auth/form/login.html')
  41. http.close()