Skip to content

Commit

Permalink
Merge pull request #250 from axw/v2-url-leading-slash
Browse files Browse the repository at this point in the history
model: add leading slash to URL path if missing
  • Loading branch information
axw authored Oct 3, 2018
2 parents dd7271f + 90b3713 commit c14a780
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
14 changes: 12 additions & 2 deletions model/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func (v *HTTPSpanContext) marshalURL(w *fastjson.Writer) bool {
if v.URL.Path == "" {
w.RawByte('/')
} else {
if v.URL.Path[0] != '/' {
w.RawByte('/')
}
w.StringContents(v.URL.Path)
}
if v.URL.RawQuery != "" {
Expand Down Expand Up @@ -122,14 +125,18 @@ func (v *URL) MarshalFastJSON(w *fastjson.Writer) {
w.String(v.Hostname)
}
if v.Path != "" {
const prefix = ",\"pathname\":"
const prefix = `,"pathname":"`
if first {
first = false
w.RawString(prefix[1:])
} else {
w.RawString(prefix)
}
w.String(v.Path)
if v.Path[0] != '/' {
w.RawByte('/')
}
w.StringContents(v.Path)
w.RawByte('"')
}
if v.Port != "" {
const prefix = ",\"port\":"
Expand Down Expand Up @@ -221,6 +228,9 @@ func (v *URL) marshalFullURL(w *fastjson.Writer, scheme []byte) bool {
w.RawByte(':')
w.StringContents(v.Port)
}
if !strings.HasPrefix(v.Path, "/") {
w.RawByte('/')
}
w.StringContents(v.Path)
if v.Search != "" {
w.RawByte('?')
Expand Down
44 changes: 44 additions & 0 deletions model/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,50 @@ func TestMarshalURL(t *testing.T) {
assert.Equal(t, in, out)
}

func TestMarshalURLPathLeadingSlashMissing(t *testing.T) {
in := model.URL{
Path: "foo",
Search: "abc=def",
Hostname: "testing.invalid",
Port: "999",
Protocol: "http",
}

var w fastjson.Writer
in.MarshalFastJSON(&w)

var out model.URL
err := json.Unmarshal(w.Bytes(), &out)
require.NoError(t, err)

assert.Equal(t, "http://testing.invalid:999/foo?abc=def", out.Full)
out.Full = ""

// Leading slash should have been added during marshalling. We do it
// here rather than when building the model to avoid allocation.
in.Path = "/foo"

assert.Equal(t, in, out)
}

func TestMarshalHTTPSpanContextURLPathLeadingSlashMissing(t *testing.T) {
httpSpanContext := model.HTTPSpanContext{
URL: mustParseURL("http://testing.invalid:8000/path?query#fragment"),
}
httpSpanContext.URL.Path = "path"

var w fastjson.Writer
httpSpanContext.MarshalFastJSON(&w)

var out model.HTTPSpanContext
err := json.Unmarshal(w.Bytes(), &out)
require.NoError(t, err)

// Leading slash should have been added during marshalling.
httpSpanContext.URL.Path = "/path"
assert.Equal(t, httpSpanContext.URL, out.URL)
}

func TestTransactionUnmarshalJSON(t *testing.T) {
tx := fakeTransaction()
var w fastjson.Writer
Expand Down

0 comments on commit c14a780

Please sign in to comment.