Skip to content

Commit

Permalink
Tests: Simplify by using URL(); test core Path
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Mar 30, 2023
1 parent 97717b9 commit 9dca126
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 106 deletions.
121 changes: 15 additions & 106 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package requests_test
import (
"bytes"
"context"
"net/http"
"net/url"
"strings"
"testing"

"github.com/carlmjohnson/requests"
"github.com/carlmjohnson/requests/internal/be"
"github.com/carlmjohnson/requests/internal/core"
)

func TestClone(t *testing.T) {
t.Parallel()
t.Run("from URL", func(t *testing.T) {
rb1 := requests.
URL("http://example.com").
Expand Down Expand Up @@ -119,126 +120,34 @@ func TestClone(t *testing.T) {
}

func TestScheme(t *testing.T) {
const res = `HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Date: Mon, 24 May 2021 18:48:50 GMT
An example response.`
var s string
const expected = `An example response.`
var trans http.Transport
trans.RegisterProtocol("string", requests.ReplayString(res))
err := requests.
u, err := requests.
URL("example").
Scheme("string").
Transport(&trans).
ToString(&s).
Fetch(context.Background())
URL()
be.NilErr(t, err)
be.Equal(t, expected, s)
be.Equal(t, "string", u.Scheme)
be.Equal(t, "example", u.Host)
be.Equal(t, "string://example", u.String())
}

func TestPath(t *testing.T) {
cases := map[string]struct {
base string
paths []string
result string
}{
"base-only": {
"example",
[]string{},
"https://example",
},
"base+abspath": {
"https://example",
[]string{"/a"},
"https://example/a",
},
"multi-abs-paths": {
"https://example",
[]string{"/a", "/b/", "/c"},
"https://example/c",
},
"base+rel-path": {
"https://example/a/",
[]string{"./b"},
"https://example/a/b",
},
"base+rel-paths": {
"https://example/a/",
[]string{"./b/", "./c"},
"https://example/a/b/c",
},
"rel-path": {
"https://example/",
[]string{"a/", "./b"},
"https://example/a/b",
},
"base+multi-paths": {
"https://example/a/",
[]string{"b/", "c"},
"https://example/a/b/c",
},
"base+slash+multi-paths": {
"https://example/a/",
[]string{"b/", "c"},
"https://example/a/b/c",
},
"multi-root": {
"https://example/",
[]string{"a", "b", "c"},
"https://example/c",
},
"dot-dot-paths": {
"https://example/",
[]string{"a/", "b/", "../c"},
"https://example/a/c",
},
"more-dot-dot-paths": {
"https://example/",
[]string{"a/b/c/", "../d/", "../e"},
"https://example/a/b/e",
},
"more-dot-dot-paths+rel-path": {
"https://example/",
[]string{"a/b/c/", "../d/", "../e/", "./f"},
"https://example/a/b/e/f",
},
"even-more-dot-dot-paths+base": {
"https://example/a/b/c/",
[]string{"../../d"},
"https://example/a/d",
},
"too-many-dot-dot-paths": {
"https://example",
[]string{"../a"},
"https://example/a",
},
"too-many-dot-dot-paths+base": {
"https://example/",
[]string{"../a"},
"https://example/a",
},
"last-abs-path-wins": {
"https://example/a/",
[]string{"b/", "c/", "/d"},
"https://example/d",
},
}
for name, tc := range cases {
t.Parallel()
for name, tc := range core.PathCases {
t.Run(name, func(t *testing.T) {
b := requests.URL(tc.base)
for _, p := range tc.paths {
var b requests.Builder
b.BaseURL(tc.Base)
for _, p := range tc.Paths {
b.Path(p)
}
r, err := b.Request(context.Background())
u, err := b.URL()
be.NilErr(t, err)
be.Equal(t, tc.result, r.URL.String())
be.Equal(t, tc.Result, u.String())
})
}
}

func TestContentLength(t *testing.T) {
t.Parallel()
for _, n := range []int{0, 1, 10, 1000, 100_000} {
req, err := requests.
URL("http://example.com").
Expand Down
1 change: 1 addition & 0 deletions errorkind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

func TestErrorKind(t *testing.T) {
t.Parallel()
var none requests.ErrorKind = -1
kinds := []requests.ErrorKind{
requests.ErrURL,
Expand Down
24 changes: 24 additions & 0 deletions internal/core/url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core_test

import (
"testing"

"github.com/carlmjohnson/requests/internal/be"
"github.com/carlmjohnson/requests/internal/core"
)

func TestPath(t *testing.T) {
t.Parallel()
for name, tc := range core.PathCases {
t.Run(name, func(t *testing.T) {
var b core.URLBuilder
b.BaseURL(tc.Base)
for _, p := range tc.Paths {
b.Path(p)
}
u, err := b.URL()
be.NilErr(t, err)
be.Equal(t, tc.Result, u.String())
})
}
}
89 changes: 89 additions & 0 deletions internal/core/url_testcases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package core

// PathCases is exported to share tests with requests
var PathCases = map[string]struct {
Base string
Paths []string
Result string
}{
"base-only": {
"example",
[]string{},
"https://example",
},
"base+abspath": {
"https://example",
[]string{"/a"},
"https://example/a",
},
"multi-abs-paths": {
"https://example",
[]string{"/a", "/b/", "/c"},
"https://example/c",
},
"base+rel-path": {
"https://example/a/",
[]string{"./b"},
"https://example/a/b",
},
"base+rel-paths": {
"https://example/a/",
[]string{"./b/", "./c"},
"https://example/a/b/c",
},
"rel-path": {
"https://example/",
[]string{"a/", "./b"},
"https://example/a/b",
},
"base+multi-paths": {
"https://example/a/",
[]string{"b/", "c"},
"https://example/a/b/c",
},
"base+slash+multi-paths": {
"https://example/a/",
[]string{"b/", "c"},
"https://example/a/b/c",
},
"multi-root": {
"https://example/",
[]string{"a", "b", "c"},
"https://example/c",
},
"dot-dot-paths": {
"https://example/",
[]string{"a/", "b/", "../c"},
"https://example/a/c",
},
"more-dot-dot-paths": {
"https://example/",
[]string{"a/b/c/", "../d/", "../e"},
"https://example/a/b/e",
},
"more-dot-dot-paths+rel-path": {
"https://example/",
[]string{"a/b/c/", "../d/", "../e/", "./f"},
"https://example/a/b/e/f",
},
"even-more-dot-dot-paths+base": {
"https://example/a/b/c/",
[]string{"../../d"},
"https://example/a/d",
},
"too-many-dot-dot-paths": {
"https://example",
[]string{"../a"},
"https://example/a",
},
"too-many-dot-dot-paths+base": {
"https://example/",
[]string{"../a"},
"https://example/a",
},
"last-abs-path-wins": {
"https://example/a/",
[]string{"b/", "c/", "/d"},
"https://example/d",
},
}

0 comments on commit 9dca126

Please sign in to comment.