Skip to content

Commit

Permalink
refactor(engine): *http.Client -> model.HTTPClient
Browse files Browse the repository at this point in the history
This diff makes the implementation of the engine package more
abstract by changing HTTPClient() to return a model.HTTPClient
as opposed to returning an *http.Client.

Part of ooni/probe#2184
  • Loading branch information
bassosimone committed Jul 8, 2022
1 parent d419ed8 commit af647ab
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
5 changes: 2 additions & 3 deletions internal/engine/mockable/mockable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mockable

import (
"context"
"net/http"
"net/url"

"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
Expand All @@ -14,7 +13,7 @@ import (
// Session allows to mock sessions.
type Session struct {
MockableTestHelpers map[string][]model.OOAPIService
MockableHTTPClient *http.Client
MockableHTTPClient model.HTTPClient
MockableLogger model.Logger
MockableMaybeResolverIP string
MockableProbeASNString string
Expand Down Expand Up @@ -45,7 +44,7 @@ func (sess *Session) GetTestHelpersByName(name string) ([]model.OOAPIService, bo
}

// DefaultHTTPClient implements ExperimentSession.DefaultHTTPClient
func (sess *Session) DefaultHTTPClient() *http.Client {
func (sess *Session) DefaultHTTPClient() model.HTTPClient {
return sess.MockableHTTPClient
}

Expand Down
3 changes: 1 addition & 2 deletions internal/engine/probeservices/probeservices.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package probeservices

import (
"errors"
"net/http"
"net/url"

"github.com/ooni/probe-cli/v3/internal/atomicx"
Expand Down Expand Up @@ -56,7 +55,7 @@ var (

// Session is how this package sees a Session.
type Session interface {
DefaultHTTPClient() *http.Client
DefaultHTTPClient() model.HTTPClient
KeyValueStore() model.KeyValueStore
Logger() model.Logger
ProxyURL() *url.URL
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (s *Session) GetTestHelpersByName(name string) ([]model.OOAPIService, bool)
}

// DefaultHTTPClient returns the session's default HTTP client.
func (s *Session) DefaultHTTPClient() *http.Client {
func (s *Session) DefaultHTTPClient() model.HTTPClient {
return &http.Client{Transport: s.httpDefaultTransport}
}

Expand Down
6 changes: 5 additions & 1 deletion internal/engine/session_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ func TestSessionByteCounter(t *testing.T) {
}
s := newSessionForTesting(t)
client := s.DefaultHTTPClient()
resp, err := client.Get("https://www.google.com")
req, err := http.NewRequest("GET", "https://www.google.com", nil)
if err != nil {
t.Fatal(err)
}
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/model/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package model

import (
"context"
"net/http"
)

//
Expand All @@ -13,7 +12,7 @@ import (
// ExperimentSession is the experiment's view of a session.
type ExperimentSession interface {
GetTestHelpersByName(name string) ([]OOAPIService, bool)
DefaultHTTPClient() *http.Client
DefaultHTTPClient() HTTPClient
FetchPsiphonConfig(ctx context.Context) ([]byte, error)
FetchTorTargets(ctx context.Context, cc string) (map[string]OOAPITorTarget, error)
FetchURLList(ctx context.Context, config OOAPIURLListConfig) ([]OOAPIURLInfo, error)
Expand Down

0 comments on commit af647ab

Please sign in to comment.