summaryrefslogtreecommitdiff
path: root/cmd/blubberoid/main_test.go
blob: 7898e75399c679478ecbbea3021ab578e3d55536 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main

import (
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

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)

	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"`)
}

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")
}