Skip to content

Commit bd3833a

Browse files
committed
URL generator also generates query and fragment
To make the generated URL conform to how `url.Parse` works, Path and Fragment are set to the unescaped form. RawPath and RawFragment are left unset, and we simply rely on the URL struct to handle the escaping as needed.
1 parent ad3b29a commit bd3833a

File tree

3 files changed

+13
-27
lines changed

3 files changed

+13
-27
lines changed

url.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ package rapid
99
import (
1010
"fmt"
1111
"net/url"
12-
"path"
1312
"reflect"
13+
"strings"
1414
"unicode"
1515
)
1616

@@ -79,6 +79,8 @@ func (g *urlGenerator) type_() reflect.Type {
7979
return urlType
8080
}
8181

82+
var printableGen = StringOf(RuneFrom(nil, unicode.PrintRanges...))
83+
8284
func (g *urlGenerator) value(t *T) value {
8385
scheme := SampledFrom(g.schemes).Draw(t, "scheme").(string)
8486
domain := Domain().Draw(t, "domain").(string)
@@ -90,15 +92,16 @@ func (g *urlGenerator) value(t *T) value {
9092
return fmt.Sprintf(":%d", i)
9193
}).
9294
Draw(t, "port").(string)
93-
path_ := path.Join(
94-
SliceOf(
95-
StringOf(RuneFrom(nil, unicode.PrintRanges...)).Map(url.PathEscape),
96-
).Draw(t, "path").([]string)...)
95+
path_ := SliceOf(printableGen).Draw(t, "path").([]string)
96+
query := SliceOf(printableGen.Map(url.QueryEscape)).Draw(t, "query").([]string)
97+
fragment := printableGen.Draw(t, "fragment").(string)
9798

9899
return url.URL{
99100
Host: domain + port,
100-
Path: path_,
101+
Path: strings.Join(path_, "/"),
101102
Scheme: scheme,
103+
RawQuery: strings.Join(query, "&"),
104+
Fragment: fragment,
102105
}
103106
}
104107

url_example_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func ExampleURL() {
3737
}
3838

3939
// Output:
40-
// https://r125pz05Rz-0j1d-11.AArP:17
41-
// http://L2.aBArTh:7/%25F0%259F%25AA%2595%2522%25D6%2593%25E0%25A9%25AD%25E1%25B3%25930%25D0%258A/%25C2%25BC%25E0%25B4%25BE3%25F0%259E%258B%25B0%25F0%2591%2586%25BD%25C2%25B2%25E0%25B3%25A9%25CC%2580D/%257C+%25F0%259F%2581%259D+%255D%25CC%2581%25CB%2585/%25CC%2580/%25E1%25B0%25BF/%25CD%2582K%25E0%25A5%25A9%25CC%2581
42-
// https://pH20DR11.aaA
43-
// http://h.AcCounTaNtS:4/%25F0%259E%25A5%259F/:%2521J%25E2%259D%2587
40+
// https://r125pz05Rz-0j1d-11.AArP:17#0%E2%81%9B%F3%A0%84%9A
41+
// http://L2.aBArTh:7/%F0%9F%AA%95%22%D6%93%E0%A9%AD%E1%B3%930%D0%8A/%C2%BC%E0%B4%BE3%F0%9E%8B%B0%F0%91%86%BD%C2%B2%E0%B3%A9%CC%80D/%7C+%F0%9F%81%9D+%5D%CC%81%CB%85/%CC%80/%E1%B0%BF/%CD%82K%E0%A5%A9%CC%81#%CC%82
42+
// https://pH20DR11.aaA?%DB%97%F0%90%AC%BC%24%F0%91%99%83%E2%82%A5%F0%9D%A8%A8%E0%BC%95%E0%B5%A8%3C%E0%BE%B0%F0%97%8D%91%E2%9E%8E%E0%B9%91%24v%CC%80&%CC%94Z%E4%87%A4#%CC%A0%E1%81%AD
43+
// http://h.AcCounTaNtS:4/%F0%9E%A5%9F/:%21J%E2%9D%87#L%CC%82%E9%98%A6%22
4444
// http://A.xN--s9bRJ9C:2
4545
}

url_external_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@ package rapid_test
88

99
import (
1010
"net/url"
11-
"regexp"
1211
"strings"
1312
"testing"
1413

1514
. "pgregory.net/rapid"
1615
)
1716

18-
var pathEscapeRegex = regexp.MustCompile(`^[0-9A-Fa-f]{2}`)
19-
2017
func TestURL(t *testing.T) {
2118
t.Parallel()
2219

@@ -27,20 +24,6 @@ func TestURL(t *testing.T) {
2724
if _, err := url.Parse(u.String()); err != nil {
2825
t.Fatalf("URL returned unparseable url %s: %v", u.String(), err)
2926
}
30-
31-
// only valid characters in path
32-
for i, ch := range u.Path {
33-
if !(('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ('0' <= ch && ch <= '9') || strings.ContainsRune("$-_.+!*'(),%/@=&:~", ch)) {
34-
t.Fatalf("URL returned invalid url %s: invalid character %s at %d", u.String(), string(ch), i)
35-
}
36-
}
37-
38-
// assert proper path escapes
39-
for _, co := range strings.Split(u.Path, "%")[1:] {
40-
if ok := pathEscapeRegex.MatchString(co); !ok {
41-
t.Fatalf("URL returned invalid url %s: invalid escape %s", u.String(), co)
42-
}
43-
}
4427
})
4528
}
4629

0 commit comments

Comments
 (0)