Added tests for client

This commit is contained in:
James Mills 2018-05-14 23:59:01 -07:00
parent 5827535f6d
commit 871e53132e
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
3 changed files with 37 additions and 4 deletions

View File

@ -38,13 +38,13 @@ image:
@echo "Image created: $(REPO):$(TAG)"
profile:
@go test -cpuprofile cpu.prof -memprofile mem.prof -v -bench=. $(TEST_ARGS)
@go test -cpuprofile cpu.prof -memprofile mem.prof -v -bench ./...
bench:
@go test -v -bench=. $(TEST_ARGS)
@go test -v -bench ./...
test:
@go test -v -cover -race $(TEST_ARGS)
@go test -v -cover -race ./...
clean:
@rm -rf $(APP)

View File

@ -153,7 +153,7 @@ func (c *Client) Publish(topic, message string) error {
return fmt.Errorf("error publishing message: %s", err)
}
if res.StatusCode != 201 {
if res.StatusCode != http.StatusAccepted {
return fmt.Errorf("unexpected response: %s", res.Status)
}

33
client/client_test.go Normal file
View File

@ -0,0 +1,33 @@
package client
import (
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/prologic/msgbus"
)
func TestClientPublish(t *testing.T) {
assert := assert.New(t)
mb := msgbus.New(nil)
server := httptest.NewServer(mb)
defer server.Close()
client := NewClient(server.URL, nil)
err := client.Publish("hello", "hello world")
assert.NoError(err)
topic := mb.NewTopic("hello")
expected := msgbus.Message{Topic: topic, Payload: []byte("hello world")}
actual, ok := mb.Get(topic)
assert.True(ok)
assert.Equal(actual.ID, expected.ID)
assert.Equal(actual.Topic, expected.Topic)
assert.Equal(actual.Payload, expected.Payload)
}