Skip to content

Commit

Permalink
Merge branch 'feature/pn-310-ttl-for-messages' into feature/pn-310-sm…
Browse files Browse the repository at this point in the history
…s-ttl
  • Loading branch information
bogh committed Mar 28, 2017
2 parents 9f6dec6 + 163654c commit da2ee2d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
9 changes: 9 additions & 0 deletions restclient/guble_sender.go
Expand Up @@ -93,11 +93,20 @@ func (gs gubleSender) Send(topic string, body []byte, userID string, params map[
"userID": userID,
"params": params,
}).Debug("Sending guble message")

request, err := http.NewRequest(http.MethodPost, getURL(gs.Endpoint, topic, userID, params), bytes.NewReader(body))
if err != nil {
return err
}

// Correlation ID header
request.Header.Add(rest.XHeaderPrefix+"correlation-id", params[correlationIDLiteral])

// Expires header
if expires, ok := params["Expires"]; ok {
request.Header.Set("Expires", expires)
}

response, err := gs.httpClient.Do(request)
if err != nil {
return err
Expand Down
15 changes: 15 additions & 0 deletions server/rest/rest_message_api.go
Expand Up @@ -3,6 +3,7 @@ package rest
import (
"errors"
"fmt"
"strconv"

"github.com/azer/snakecase"

Expand Down Expand Up @@ -102,6 +103,7 @@ func (api *RestMessageAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Path: protocol.Path(topic),
Body: body,
UserID: q(r, "userId"),
Expires: extractExpiresHeader(r),
ApplicationID: xid.New().String(),
HeaderJSON: headersToJSON(r.Header),
}
Expand All @@ -113,6 +115,19 @@ func (api *RestMessageAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "OK")
}

// extractExpiresHeader will return the value of the `Expires` Header if is set or 0
func extractExpiresHeader(r *http.Request) int64 {
expiresHeader := r.Header.Get("Expires")
if expiresHeader == "" {
return 0
}
v, err := strconv.ParseInt(expiresHeader, 10, 64)
if err != nil {
return 0
}
return v
}

func (api *RestMessageAPI) extractTopic(path string, requestTypeTopicPrefix string) (string, error) {
p := removeTrailingSlash(api.prefix) + requestTypeTopicPrefix
if !strings.HasPrefix(path, p) {
Expand Down
27 changes: 26 additions & 1 deletion server/rest/rest_message_api_test.go
Expand Up @@ -10,13 +10,14 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/cosminrentea/go-uuid"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"

"github.com/cosminrentea/go-uuid"
)

var testBytes = []byte("test")
Expand Down Expand Up @@ -151,6 +152,30 @@ func TestHeadersToJSON(t *testing.T) {
a.Equal("y", header["x"])
}

func TestExtractExpires(t *testing.T) {
a := assert.New(t)
cases := []struct {
header string
expected int64
}{
{"1420110000", 1420110000},
{"", 0},
}

for i, c := range cases {
r, err := http.NewRequest(http.MethodPost, "http://dummyurl/", nil)
a.NoError(err)
r.Header.Set("Expires", c.header)

a.Equal(c.expected, extractExpiresHeader(r), "Failed extractExpiresHeader for case: %d", i)
}

// no header case
r, err := http.NewRequest(http.MethodPost, "http://dummyurl/", nil)
a.NoError(err)
a.Equal(int64(0), extractExpiresHeader(r))
}

func TestRemoveTrailingSlash(t *testing.T) {
assert.Equal(t, "/foo", removeTrailingSlash("/foo/"))
assert.Equal(t, "/foo", removeTrailingSlash("/foo"))
Expand Down

0 comments on commit da2ee2d

Please sign in to comment.