-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(httpclientx): couple URL and cloudfront host header (#1584)
Closes ooni/probe#2727.
- Loading branch information
1 parent
1056c98
commit 9dbe15c
Showing
27 changed files
with
549 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package httpclientx | ||
|
||
import "github.com/ooni/probe-cli/v3/internal/model" | ||
|
||
// Endpoint is an HTTP endpoint. | ||
// | ||
// The zero value is invalid; construct using [NewEndpoint]. | ||
type Endpoint struct { | ||
// URL is the MANDATORY endpoint URL. | ||
URL string | ||
|
||
// Host is the OPTIONAL host header to use for cloudfronting. | ||
Host string | ||
} | ||
|
||
// NewEndpoint constructs a new [*Endpoint] instance using the given URL. | ||
func NewEndpoint(URL string) *Endpoint { | ||
return &Endpoint{ | ||
URL: URL, | ||
Host: "", | ||
} | ||
} | ||
|
||
// WithHostOverride returns a copy of the [*Endpoint] using the given host header override. | ||
func (e *Endpoint) WithHostOverride(host string) *Endpoint { | ||
return &Endpoint{ | ||
URL: e.URL, | ||
Host: host, | ||
} | ||
} | ||
|
||
// NewEndpointFromModelOOAPIServices constructs new [*Endpoint] instances from the | ||
// given [model.OOAPIService] instances, assigning the host header if "cloudfront", and | ||
// skipping all the entries that are neither "https" not "cloudfront". | ||
func NewEndpointFromModelOOAPIServices(svcs ...model.OOAPIService) (epnts []*Endpoint) { | ||
for _, svc := range svcs { | ||
epnt := NewEndpoint(svc.Address) | ||
switch svc.Type { | ||
case "cloudfront": | ||
epnt = epnt.WithHostOverride(svc.Front) | ||
fallthrough | ||
case "https": | ||
epnts = append(epnts, epnt) | ||
default: | ||
// skip entry | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package httpclientx | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/ooni/probe-cli/v3/internal/model" | ||
) | ||
|
||
func TestEndpoint(t *testing.T) { | ||
t.Run("the constructor only assigns the URL", func(t *testing.T) { | ||
epnt := NewEndpoint("https://www.example.com/") | ||
if epnt.URL != "https://www.example.com/" { | ||
t.Fatal("unexpected URL") | ||
} | ||
if epnt.Host != "" { | ||
t.Fatal("unexpected host") | ||
} | ||
}) | ||
|
||
t.Run("we can optionally get a copy with an assigned host header", func(t *testing.T) { | ||
epnt := NewEndpoint("https://www.example.com/").WithHostOverride("www.cloudfront.com") | ||
if epnt.URL != "https://www.example.com/" { | ||
t.Fatal("unexpected URL") | ||
} | ||
if epnt.Host != "www.cloudfront.com" { | ||
t.Fatal("unexpected host") | ||
} | ||
}) | ||
|
||
t.Run("we can convert from model.OOAPIService", func(t *testing.T) { | ||
services := []model.OOAPIService{{ | ||
Address: "", | ||
Type: "onion", | ||
Front: "", | ||
}, { | ||
Address: "https://www.example.com/", | ||
Type: "https", | ||
}, { | ||
Address: "", | ||
Type: "onion", | ||
Front: "", | ||
}, { | ||
Address: "https://www.example.com/", | ||
Type: "cloudfront", | ||
Front: "www.cloudfront.com", | ||
}, { | ||
Address: "", | ||
Type: "onion", | ||
Front: "", | ||
}} | ||
|
||
expect := []*Endpoint{{ | ||
URL: "https://www.example.com/", | ||
}, { | ||
URL: "https://www.example.com/", | ||
Host: "www.cloudfront.com", | ||
}} | ||
|
||
got := NewEndpointFromModelOOAPIServices(services...) | ||
if diff := cmp.Diff(expect, got); diff != "" { | ||
t.Fatal(diff) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.