Esempio n. 1
0
func TestMkstream(t *testing.T) {
	request, _ := http.NewRequest("POST", "/streams", nil)
	response := httptest.NewRecorder()

	mkstream(response, request)

	assert.Equal(t, response.Code, 200)
	assert.Len(t, response.Body.String(), 32)
}
Esempio n. 2
0
func TestPubSub(t *testing.T) {
	server := httptest.NewServer(app())
	defer server.Close()

	data := [][]byte{
		[]byte{'h', 'e', 'l', 'l', 'o'},
		[]byte{0x1f, 0x8b, 0x08, 0x00, 0x3f, 0x6b, 0xe1, 0x53, 0x00, 0x03, 0xed, 0xce, 0xb1, 0x0a, 0xc2, 0x30},
		bytes.Repeat([]byte{'0'}, 32769),
	}

	for _, expected := range data {
		// uuid = curl -XPOST <url>/streams
		resp, err := http.Post(server.URL+"/streams", "", nil)
		defer resp.Body.Close()
		assert.Nil(t, err)

		body, err := ioutil.ReadAll(resp.Body)
		assert.Nil(t, err)

		// uuid extracted
		uuid := string(body)
		assert.Len(t, uuid, 32)

		done := make(chan bool)

		go func() {
			// curl <url>/streams/<uuid>
			// -- waiting for publish to arrive
			resp, err = http.Get(server.URL + "/streams/" + uuid)
			defer resp.Body.Close()
			assert.Nil(t, err)

			body, _ = ioutil.ReadAll(resp.Body)
			assert.Equal(t, body, expected)

			done <- true
		}()

		transport := &http.Transport{}
		client := &http.Client{Transport: transport}

		// curl -XPOST -H "Transfer-Encoding: chunked" -d "hello" <url>/streams/<uuid>
		req, _ := http.NewRequest("POST", server.URL+"/streams/"+uuid, bytes.NewReader(expected))
		req.TransferEncoding = []string{"chunked"}
		r, err := client.Do(req)
		r.Body.Close()
		assert.Nil(t, err)

		<-done

		// Read the whole response after the publisher has
		// completed. The mechanics of this is different in that
		// most of the content will be replayed instead of received
		// in chunks as they arrive.
		resp, err = http.Get(server.URL + "/streams/" + uuid)
		defer resp.Body.Close()
		assert.Nil(t, err)

		body, _ = ioutil.ReadAll(resp.Body)
		assert.Equal(t, body, expected)
	}
}