Improve API to be more RESTful
This commit is contained in:
parent
9d131ac06f
commit
c640130f11
|
@ -38,13 +38,6 @@ const (
|
|||
pingPeriod = (pongWait * 9) / 10
|
||||
)
|
||||
|
||||
var (
|
||||
// PublishedRegexp ...
|
||||
PublishedRegexp = regexp.MustCompile(
|
||||
"message successfully published to \\w+ with sequence \\d",
|
||||
)
|
||||
)
|
||||
|
||||
// Client ...
|
||||
type Client struct {
|
||||
url string
|
||||
|
@ -162,17 +155,8 @@ func (c *Client) Publish(topic, message string) error {
|
|||
return fmt.Errorf("error publishing message: %s", err)
|
||||
}
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
return fmt.Errorf("unexpected non-200 response: %s", res.Status)
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading response: %s", err)
|
||||
}
|
||||
|
||||
if !PublishedRegexp.Match(body) {
|
||||
return fmt.Errorf("unexpected non-matching response: %s", body)
|
||||
if res.StatusCode != 201 {
|
||||
return fmt.Errorf("unexpected response: %s", res.Status)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
11
msgbus.go
11
msgbus.go
|
@ -497,6 +497,7 @@ func (mb *MessageBus) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(out)
|
||||
return
|
||||
|
@ -527,14 +528,9 @@ func (mb *MessageBus) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
message := mb.NewMessage(t, body)
|
||||
mb.Put(message)
|
||||
mb.Put(mb.NewMessage(t, body))
|
||||
|
||||
msg := fmt.Sprintf(
|
||||
"message successfully published to %s with sequence %d",
|
||||
t.Name, message.ID,
|
||||
)
|
||||
w.Write([]byte(msg))
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
case "GET":
|
||||
if r.Header.Get("Upgrade") == "websocket" {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
|
@ -562,6 +558,7 @@ func (mb *MessageBus) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(out)
|
||||
case "DELETE":
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
|
@ -74,8 +73,7 @@ func TestServeHTTPPOST(t *testing.T) {
|
|||
r, _ := http.NewRequest("POST", "/hello", b)
|
||||
|
||||
mb.ServeHTTP(w, r)
|
||||
assert.Equal(w.Code, http.StatusOK)
|
||||
assert.Regexp(`message successfully published to hello with sequence \d+`, w.Body.String())
|
||||
assert.Equal(w.Code, http.StatusAccepted)
|
||||
}
|
||||
|
||||
func TestServeHTTPMaxPayloadSize(t *testing.T) {
|
||||
|
@ -101,8 +99,7 @@ func TestServeHTTPSimple(t *testing.T) {
|
|||
r, _ := http.NewRequest("POST", "/hello", b)
|
||||
|
||||
mb.ServeHTTP(w, r)
|
||||
assert.Equal(w.Code, http.StatusOK)
|
||||
assert.Regexp(`message successfully published to hello with sequence \d+`, w.Body.String())
|
||||
assert.Equal(w.Code, http.StatusAccepted)
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
r, _ = http.NewRequest("GET", "/hello", nil)
|
||||
|
@ -167,9 +164,6 @@ func TestServeHTTPSubscriber(t *testing.T) {
|
|||
r, err := c.Post(s.URL+"/hello", "text/plain", b)
|
||||
assert.NoError(err)
|
||||
defer r.Body.Close()
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
assert.NoError(err)
|
||||
assert.Regexp(`message successfully published to hello with sequence \d+`, string(body))
|
||||
|
||||
msg := <-msgs
|
||||
assert.Equal(msg.ID, uint64(0))
|
||||
|
|
Loading…
Reference in New Issue