Fix response for empty topic/queue by responding with HTTP 204 No Content instead of 404 Not Found (Fixes #25)

Этот коммит содержится в:
James Mills 2022-03-31 20:55:03 +10:00
родитель a5ef82800e
Коммит 929cfd3a36
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: AC4C014F1440EBD6
4 изменённых файлов: 5 добавлений и 5 удалений

Просмотреть файл

@ -93,7 +93,8 @@ func (c *Client) Pull(topic string) (msg *msgbus.Message, err error) {
return nil, err
}
if res.StatusCode == http.StatusNotFound {
// XXX: StatusNotFound is for backwards compatibility only for older clients.
if res.StatusCode == http.StatusNoContent || res.StatusCode == http.StatusNotFound {
// Empty queue
return nil, nil
}

2
go.mod
Просмотреть файл

@ -1,6 +1,6 @@
module git.mills.io/prologic/msgbus
go 1.17
go 1.18
require (
github.com/gorilla/websocket v1.5.0

Просмотреть файл

@ -567,8 +567,7 @@ func (mb *MessageBus) ServeHTTP(w http.ResponseWriter, r *http.Request) {
message, ok := mb.Get(t)
if !ok {
msg := fmt.Sprintf("no messages enqueued for topic: %s", topic)
http.Error(w, msg, http.StatusNotFound)
http.Error(w, "No Messages", http.StatusNoContent)
return
}

Просмотреть файл

@ -89,7 +89,7 @@ func TestServeHTTPGETEmptyQueue(t *testing.T) {
r, _ := http.NewRequest("GET", "/hello", nil)
mb.ServeHTTP(w, r)
assert.Equal(w.Code, http.StatusNotFound)
assert.Equal(w.Code, http.StatusNoContent)
}
func TestServeHTTPPOST(t *testing.T) {