Skip to content

Commit

Permalink
Rename HTTP (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe authored Nov 10, 2021
1 parent 5961bb8 commit 0a58e1e
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 121 deletions.
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c *command) runWithError(ss []string) (bool, error) {
return false, errors.New("verbose option not supported for JSON output")
}

client := newThrottledHTTPClient(
client := newThrottledHttpClient(
c.httpClientFactory.Create(
httpClientOptions{
MaxConnectionsPerHost: args.MaxConnectionsPerHost,
Expand Down
58 changes: 29 additions & 29 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,38 @@ import (
"github.com/stretchr/testify/assert"
)

func newTestCommand(h func(*url.URL) (*fakeHTTPResponse, error)) *command {
func newTestCommand(h func(*url.URL) (*fakeHttpResponse, error)) *command {
return newTestCommandWithStdout(io.Discard, h)
}

func newTestCommandWithStdout(stdout io.Writer, h func(*url.URL) (*fakeHTTPResponse, error)) *command {
func newTestCommandWithStdout(stdout io.Writer, h func(*url.URL) (*fakeHttpResponse, error)) *command {
return newCommand(
stdout,
io.Discard,
false,
newFakeHTTPClientFactory(h),
newFakeHttpClientFactory(h),
)
}

func newTestCommandWithStderr(stderr io.Writer, h func(*url.URL) (*fakeHTTPResponse, error)) *command {
func newTestCommandWithStderr(stderr io.Writer, h func(*url.URL) (*fakeHttpResponse, error)) *command {
return newCommand(
io.Discard,
stderr,
false,
newFakeHTTPClientFactory(h),
newFakeHttpClientFactory(h),
)
}

func TestCommandRun(t *testing.T) {
ok := newTestCommand(
func(u *url.URL) (*fakeHTTPResponse, error) {
func(u *url.URL) (*fakeHttpResponse, error) {
s := "http://foo.com"

if u.String() != s {
return nil, errors.New("")
}

return newFakeHTTPResponse(200, s, "text/html", nil), nil
return newFakeHttpResponse(200, s, "text/html", nil), nil
},
).Run([]string{"http://foo.com"})

Expand All @@ -55,18 +55,18 @@ func TestCommandRunWithLinks(t *testing.T) {
visited := false

ok := newTestCommand(
func(u *url.URL) (*fakeHTTPResponse, error) {
func(u *url.URL) (*fakeHttpResponse, error) {
switch u.String() {
case "http://foo.com":
return newFakeHTTPResponse(
return newFakeHttpResponse(
200,
"http://foo.com",
"text/html",
[]byte(`<html><body><a href="/foo" /></body></html>`),
), nil
case "http://foo.com/foo":
visited = true
return newFakeHTTPResponse(200, "http://foo.com", "text/html", nil), nil
return newFakeHttpResponse(200, "http://foo.com", "text/html", nil), nil
}

return nil, errors.New("")
Expand All @@ -82,8 +82,8 @@ func TestCommandRunWithVerboseOption(t *testing.T) {

ok := newTestCommandWithStdout(
b,
func(u *url.URL) (*fakeHTTPResponse, error) {
return newFakeHTTPResponse(200, "http://foo.com", "text/html", nil), nil
func(u *url.URL) (*fakeHttpResponse, error) {
return newFakeHttpResponse(200, "http://foo.com", "text/html", nil), nil
},
).Run([]string{"-v", "http://foo.com"})

Expand All @@ -93,7 +93,7 @@ func TestCommandRunWithVerboseOption(t *testing.T) {

func TestCommandFailToRun(t *testing.T) {
ok := newTestCommand(
func(u *url.URL) (*fakeHTTPResponse, error) {
func(u *url.URL) (*fakeHttpResponse, error) {
return nil, errors.New("")
},
).Run([]string{"http://foo.com"})
Expand All @@ -106,9 +106,9 @@ func TestCommandFailToRunWithInvalidLink(t *testing.T) {

ok := newTestCommandWithStdout(
b,
func(u *url.URL) (*fakeHTTPResponse, error) {
func(u *url.URL) (*fakeHttpResponse, error) {
if u.String() == "http://foo.com" {
return newFakeHTTPResponse(
return newFakeHttpResponse(
200,
"http://foo.com",
"text/html",
Expand All @@ -129,8 +129,8 @@ func TestCommandFailToParseArguments(t *testing.T) {

c := newTestCommandWithStderr(
b,
func(u *url.URL) (*fakeHTTPResponse, error) {
return newFakeHTTPResponse(200, "", "text/html", nil), nil
func(u *url.URL) (*fakeHttpResponse, error) {
return newFakeHttpResponse(200, "", "text/html", nil), nil
},
)

Expand All @@ -145,7 +145,7 @@ func TestCommandFailToFetchRootPage(t *testing.T) {

ok := newTestCommandWithStderr(
b,
func(u *url.URL) (*fakeHTTPResponse, error) {
func(u *url.URL) (*fakeHttpResponse, error) {
return nil, errors.New("foo")
},
).Run([]string{"http://foo.com"})
Expand All @@ -159,8 +159,8 @@ func TestCommandFailToGetHTMLRootPage(t *testing.T) {

ok := newTestCommandWithStderr(
b,
func(u *url.URL) (*fakeHTTPResponse, error) {
return newFakeHTTPResponse(200, "", "image/png", nil), nil
func(u *url.URL) (*fakeHttpResponse, error) {
return newFakeHttpResponse(200, "", "image/png", nil), nil
},
).Run([]string{"http://foo.com"})

Expand All @@ -175,7 +175,7 @@ func TestCommandColorErrorMessage(t *testing.T) {
io.Discard,
b,
true,
newFakeHTTPClientFactory(func(u *url.URL) (*fakeHTTPResponse, error) {
newFakeHttpClientFactory(func(u *url.URL) (*fakeHttpResponse, error) {
return nil, errors.New("foo")
}),
)
Expand All @@ -191,8 +191,8 @@ func TestCommandShowHelp(t *testing.T) {

c := newTestCommandWithStdout(
b,
func(u *url.URL) (*fakeHTTPResponse, error) {
return newFakeHTTPResponse(200, "", "text/html", nil), nil
func(u *url.URL) (*fakeHttpResponse, error) {
return newFakeHttpResponse(200, "", "text/html", nil), nil
},
)

Expand All @@ -207,8 +207,8 @@ func TestCommandShowVersion(t *testing.T) {

c := newTestCommandWithStdout(
b,
func(u *url.URL) (*fakeHTTPResponse, error) {
return newFakeHTTPResponse(200, "", "text/html", nil), nil
func(u *url.URL) (*fakeHttpResponse, error) {
return newFakeHttpResponse(200, "", "text/html", nil), nil
},
)

Expand All @@ -225,9 +225,9 @@ func TestCommandFailToRunWithJSONOutput(t *testing.T) {

ok := newTestCommandWithStdout(
b,
func(u *url.URL) (*fakeHTTPResponse, error) {
func(u *url.URL) (*fakeHttpResponse, error) {
if u.String() == "http://foo.com" {
return newFakeHTTPResponse(
return newFakeHttpResponse(
200,
"http://foo.com",
"text/html",
Expand All @@ -248,8 +248,8 @@ func TestCommandDoNotIncludeSuccessfulPageInJSONOutput(t *testing.T) {

ok := newTestCommandWithStdout(
b,
func(u *url.URL) (*fakeHTTPResponse, error) {
return newFakeHTTPResponse(200, "", "text/html", nil), nil
func(u *url.URL) (*fakeHttpResponse, error) {
return newFakeHttpResponse(200, "", "text/html", nil), nil
},
).Run([]string{"--json", "http://foo.com"})

Expand Down
12 changes: 6 additions & 6 deletions fake_http_client_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package main

import "net/url"

type fakeHTTPClientFactory struct {
handler func(*url.URL) (*fakeHTTPResponse, error)
type fakeHttpClientFactory struct {
handler func(*url.URL) (*fakeHttpResponse, error)
}

func newFakeHTTPClientFactory(h func(*url.URL) (*fakeHTTPResponse, error)) httpClientFactory {
return &fakeHTTPClientFactory{h}
func newFakeHttpClientFactory(h func(*url.URL) (*fakeHttpResponse, error)) httpClientFactory {
return &fakeHttpClientFactory{h}
}

func (f *fakeHTTPClientFactory) Create(httpClientOptions) httpClient {
return newFakeHTTPClient(f.handler)
func (f *fakeHttpClientFactory) Create(httpClientOptions) httpClient {
return newFakeHttpClient(f.handler)
}
10 changes: 5 additions & 5 deletions fake_http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"net/url"
)

type fakeHTTPClient struct {
handler func(*url.URL) (*fakeHTTPResponse, error)
type fakeHttpClient struct {
handler func(*url.URL) (*fakeHttpResponse, error)
}

func newFakeHTTPClient(h func(*url.URL) (*fakeHTTPResponse, error)) *fakeHTTPClient {
return &fakeHTTPClient{h}
func newFakeHttpClient(h func(*url.URL) (*fakeHttpResponse, error)) *fakeHttpClient {
return &fakeHttpClient{h}
}

func (c *fakeHTTPClient) Get(u *url.URL) (httpResponse, error) {
func (c *fakeHttpClient) Get(u *url.URL) (httpResponse, error) {
return c.handler(u)
}
14 changes: 7 additions & 7 deletions fake_http_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ package main

import "strings"

type fakeHTTPResponse struct {
type fakeHttpResponse struct {
statusCode int
location string
contentType string
body []byte
}

func newFakeHTTPResponse(statusCode int, location string, contentType string, body []byte) *fakeHTTPResponse {
return &fakeHTTPResponse{statusCode, location, contentType, body}
func newFakeHttpResponse(statusCode int, location string, contentType string, body []byte) *fakeHttpResponse {
return &fakeHttpResponse{statusCode, location, contentType, body}
}

func (r *fakeHTTPResponse) URL() string {
func (r *fakeHttpResponse) URL() string {
return r.location
}

func (r *fakeHTTPResponse) StatusCode() int {
func (r *fakeHttpResponse) StatusCode() int {
return r.statusCode
}

func (r *fakeHTTPResponse) Header(name string) string {
func (r *fakeHttpResponse) Header(name string) string {
if strings.ToLower(name) == "content-type" {
return r.contentType
}

return ""
}

func (r *fakeHTTPResponse) Body() ([]byte, error) {
func (r *fakeHttpResponse) Body() ([]byte, error) {
return r.body, nil
}
10 changes: 5 additions & 5 deletions fasthttp_http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import (
"github.com/valyala/fasthttp"
)

type fasthttpHTTPClient struct {
type fasthttpHttpClient struct {
client *fasthttp.Client
maxRedirections int
timeout time.Duration
headers map[string]string
}

func newFasthttpHTTPClient(c *fasthttp.Client, maxRedirections int, timeout time.Duration, headers map[string]string) httpClient {
return &fasthttpHTTPClient{c, maxRedirections, timeout, headers}
func newFasthttpHttpClient(c *fasthttp.Client, maxRedirections int, timeout time.Duration, headers map[string]string) httpClient {
return &fasthttpHttpClient{c, maxRedirections, timeout, headers}
}

func (c *fasthttpHTTPClient) Get(u *url.URL) (httpResponse, error) {
func (c *fasthttpHttpClient) Get(u *url.URL) (httpResponse, error) {
req, res := fasthttp.Request{}, fasthttp.Response{}
req.SetRequestURI(u.String())
req.SetConnectionClose()
Expand All @@ -47,7 +47,7 @@ func (c *fasthttpHTTPClient) Get(u *url.URL) (httpResponse, error) {

switch res.StatusCode() / 100 {
case 2:
return newFasthttpHTTPResponse(req.URI(), &res), nil
return newFasthttpHttpResponse(req.URI(), &res), nil
case 3:
i++

Expand Down
10 changes: 5 additions & 5 deletions fasthttp_http_client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"github.com/valyala/fasthttp/fasthttpproxy"
)

type fasthttpHTTPClientFactory struct {
type fasthttpHttpClientFactory struct {
}

func newFasthttpHTTPClientFactory() *fasthttpHTTPClientFactory {
return &fasthttpHTTPClientFactory{}
func newFasthttpHttpClientFactory() *fasthttpHttpClientFactory {
return &fasthttpHttpClientFactory{}
}

func (*fasthttpHTTPClientFactory) Create(o httpClientOptions) httpClient {
func (*fasthttpHttpClientFactory) Create(o httpClientOptions) httpClient {
d := func(addr string) (net.Conn, error) {
return fasthttp.DialTimeout(addr, tcpTimeout)
}
Expand All @@ -24,7 +24,7 @@ func (*fasthttpHTTPClientFactory) Create(o httpClientOptions) httpClient {
d = fasthttpproxy.FasthttpHTTPDialerTimeout(o.Proxy, tcpTimeout)
}

return newFasthttpHTTPClient(
return newFasthttpHttpClient(
&fasthttp.Client{
MaxConnsPerHost: o.MaxConnectionsPerHost,
ReadBufferSize: o.BufferSize,
Expand Down
14 changes: 7 additions & 7 deletions fasthttp_http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ import (
"github.com/valyala/fasthttp"
)

type fasthttpHTTPResponse struct {
type fasthttpHttpResponse struct {
url *fasthttp.URI
response *fasthttp.Response
}

func newFasthttpHTTPResponse(u *fasthttp.URI, r *fasthttp.Response) httpResponse {
return fasthttpHTTPResponse{u, r}
func newFasthttpHttpResponse(u *fasthttp.URI, r *fasthttp.Response) httpResponse {
return fasthttpHttpResponse{u, r}
}

func (r fasthttpHTTPResponse) URL() string {
func (r fasthttpHttpResponse) URL() string {
return r.url.String()
}

func (r fasthttpHTTPResponse) StatusCode() int {
func (r fasthttpHttpResponse) StatusCode() int {
return r.response.StatusCode()
}

func (r fasthttpHTTPResponse) Header(key string) string {
func (r fasthttpHttpResponse) Header(key string) string {
return string(r.response.Header.Peek(key))
}

func (r fasthttpHTTPResponse) Body() ([]byte, error) {
func (r fasthttpHttpResponse) Body() ([]byte, error) {
switch string(r.response.Header.Peek("Content-Encoding")) {
case "gzip":
return r.response.BodyGunzip()
Expand Down
Loading

0 comments on commit 0a58e1e

Please sign in to comment.