Added tests for client
This commit is contained in:
parent
5827535f6d
commit
871e53132e
6
Makefile
6
Makefile
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue