Capstan is a Golang web framework that shares some similarities with others in its segment.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.5KB

  1. // This file tests the method mangler middleware used to supplement HTTP methods
  2. // used by browsers that disallow certain method types, such as PUT or PATCH.
  3. //
  4. // This file may need to be moved to the middleware package.
  5. package middleware_test
  6. import (
  7. "bytes"
  8. "testing"
  9. "git.destrealm.org/go/capstan"
  10. "git.destrealm.org/go/capstan/middleware"
  11. "git.destrealm.org/go/capstan/tests"
  12. "git.destrealm.org/go/please"
  13. )
  14. type MethodManglerController struct {
  15. capstan.BaseController
  16. }
  17. func (c *MethodManglerController) Post(ctx capstan.Context) error {
  18. _, err := ctx.Write([]byte(ctx.Request().Method))
  19. return err
  20. }
  21. func Test_MangleMethod(t *testing.T) {
  22. server := tests.NewServer(func(app capstan.Application) {
  23. app.Bind(&MethodManglerController{
  24. capstan.BaseController{
  25. Path: "/mangle",
  26. Middleware: capstan.Middleware{middleware.MangleHeader("x-method")},
  27. },
  28. })
  29. })
  30. defer server.Close()
  31. requester := please.MakeRequest(server.URL)
  32. requester.SetClient(server.Client())
  33. req := requester.Post("/mangle")
  34. req.Headers().Add("x-method", "put")
  35. req.Body(bytes.NewBuffer([]byte{}))
  36. response, err := req.Commit()
  37. if err != nil {
  38. t.Fatalf("unexpected error to response: %v", err)
  39. }
  40. if response.StatusCode != 200 {
  41. t.Errorf("expected status code 200 but received %d", response.StatusCode)
  42. }
  43. buf, err := response.ReadAll()
  44. if err != nil {
  45. t.Fatalf("unable to read response: %v", err)
  46. }
  47. if string(buf) != "PUT" {
  48. t.Errorf(`expected request method "PUT" but got "%s"`, string(buf))
  49. }
  50. }