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.

89 lines
2.4 KiB

  1. package main
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. "net/http/httptest"
  6. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestBlubberoidYAMLRequest(t *testing.T) {
  11. rec := httptest.NewRecorder()
  12. req := httptest.NewRequest("POST", "/v1/test", strings.NewReader(`---
  13. version: v4
  14. base: foo
  15. variants:
  16. test: {}`))
  17. req.Header.Set("Content-Type", "application/yaml")
  18. blubberoid(rec, req)
  19. resp := rec.Result()
  20. body, _ := ioutil.ReadAll(resp.Body)
  21. assert.Equal(t, http.StatusOK, resp.StatusCode)
  22. assert.Equal(t, "text/plain", resp.Header.Get("Content-Type"))
  23. assert.Contains(t, string(body), "FROM foo")
  24. assert.Contains(t, string(body), `LABEL blubber.variant="test"`)
  25. }
  26. func TestBlubberoidJSONRequest(t *testing.T) {
  27. t.Run("valid JSON syntax", func(t *testing.T) {
  28. rec := httptest.NewRecorder()
  29. req := httptest.NewRequest("POST", "/v1/test", strings.NewReader(`{
  30. "version": "v4",
  31. "base": "foo",
  32. "variants": {
  33. "test": {}
  34. }
  35. }`))
  36. req.Header.Set("Content-Type", "application/json")
  37. blubberoid(rec, req)
  38. resp := rec.Result()
  39. body, _ := ioutil.ReadAll(resp.Body)
  40. assert.Equal(t, http.StatusOK, resp.StatusCode)
  41. assert.Equal(t, "text/plain", resp.Header.Get("Content-Type"))
  42. assert.Contains(t, string(body), "FROM foo")
  43. assert.Contains(t, string(body), `LABEL blubber.variant="test"`)
  44. })
  45. t.Run("invalid JSON syntax", func(t *testing.T) {
  46. rec := httptest.NewRecorder()
  47. req := httptest.NewRequest("POST", "/v1/test", strings.NewReader(`{
  48. version: "v4",
  49. base: "foo",
  50. variants: {
  51. test: {},
  52. },
  53. }`))
  54. req.Header.Set("Content-Type", "application/json")
  55. blubberoid(rec, req)
  56. resp := rec.Result()
  57. body, _ := ioutil.ReadAll(resp.Body)
  58. assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
  59. assert.Equal(t, string(body), "Failed to read 'application/json' config from request body. Error: invalid character 'v' looking for beginning of object key string\n")
  60. })
  61. }
  62. func TestBlubberoidUnsupportedMediaType(t *testing.T) {
  63. rec := httptest.NewRecorder()
  64. req := httptest.NewRequest("POST", "/v1/test", strings.NewReader(``))
  65. req.Header.Set("Content-Type", "application/foo")
  66. blubberoid(rec, req)
  67. resp := rec.Result()
  68. body, _ := ioutil.ReadAll(resp.Body)
  69. assert.Equal(t, http.StatusUnsupportedMediaType, resp.StatusCode)
  70. assert.Equal(t, string(body), "'application/foo' media type is not supported\n")
  71. }