diff options
Diffstat (limited to 'cmd/blubberoid/main_test.go')
-rw-r--r-- | cmd/blubberoid/main_test.go | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/cmd/blubberoid/main_test.go b/cmd/blubberoid/main_test.go index 0d7c0d3..7898e75 100644 --- a/cmd/blubberoid/main_test.go +++ b/cmd/blubberoid/main_test.go @@ -10,13 +10,14 @@ import ( "github.com/stretchr/testify/assert" ) -func TestBlubberoid(t *testing.T) { +func TestBlubberoidYAMLRequest(t *testing.T) { rec := httptest.NewRecorder() req := httptest.NewRequest("POST", "/test", strings.NewReader(`--- version: v3 base: foo variants: test: {}`)) + req.Header.Set("Content-Type", "application/yaml") blubberoid(rec, req) @@ -28,3 +29,61 @@ func TestBlubberoid(t *testing.T) { assert.Contains(t, string(body), "FROM foo") assert.Contains(t, string(body), `LABEL blubber.variant="test"`) } + +func TestBlubberoidJSONRequest(t *testing.T) { + t.Run("valid JSON syntax", func(t *testing.T) { + rec := httptest.NewRecorder() + req := httptest.NewRequest("POST", "/test", strings.NewReader(`{ + "version": "v3", + "base": "foo", + "variants": { + "test": {} + } + }`)) + req.Header.Set("Content-Type", "application/json") + + blubberoid(rec, req) + + resp := rec.Result() + body, _ := ioutil.ReadAll(resp.Body) + + assert.Equal(t, http.StatusOK, resp.StatusCode) + assert.Equal(t, "text/plain", resp.Header.Get("Content-Type")) + assert.Contains(t, string(body), "FROM foo") + assert.Contains(t, string(body), `LABEL blubber.variant="test"`) + }) + + t.Run("invalid JSON syntax", func(t *testing.T) { + rec := httptest.NewRecorder() + req := httptest.NewRequest("POST", "/test", strings.NewReader(`{ + version: "v3", + base: "foo", + variants: { + test: {}, + }, + }`)) + req.Header.Set("Content-Type", "application/json") + + blubberoid(rec, req) + + resp := rec.Result() + body, _ := ioutil.ReadAll(resp.Body) + + assert.Equal(t, http.StatusBadRequest, resp.StatusCode) + assert.Equal(t, string(body), "'application/json' media type given but request contains invalid JSON\n") + }) +} + +func TestBlubberoidUnsupportedMediaType(t *testing.T) { + rec := httptest.NewRecorder() + req := httptest.NewRequest("POST", "/test", strings.NewReader(``)) + req.Header.Set("Content-Type", "application/foo") + + blubberoid(rec, req) + + resp := rec.Result() + body, _ := ioutil.ReadAll(resp.Body) + + assert.Equal(t, http.StatusUnsupportedMediaType, resp.StatusCode) + assert.Equal(t, string(body), "'application/foo' media type is not supported\n") +} |