Skip to content

Commit

Permalink
Fix offline mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bpapillon committed Oct 26, 2024
1 parent 97296e7 commit 3d7dee6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
29 changes: 27 additions & 2 deletions client/schematic_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,33 @@ func TestMultipleEventBatches(t *testing.T) {
time.Sleep(20 * time.Millisecond)
}

func TestCheckFlagOfflineMode(t *testing.T) {
client := schematicclient.NewSchematicClient()
func TestCheckFlagOfflineModeNoAPIKeyOption(t *testing.T) {
// Passing mock HTTP client, which we expect to be replaced by a NoopClient internally. This test will fail if any calls are made to the mock client.
ctrl := gomock.NewController(t)
mockHTTPClient := mocks.NewMockHTTPClient(ctrl)
client := schematicclient.NewSchematicClient(option.WithHTTPClient(mockHTTPClient))
client.SetFlagDefault("test-flag", true)
defer client.Close()

assert.True(t, client.CheckFlag(context.Background(), &schematicgo.CheckFlagRequestBody{}, "test-flag"))
}

func TestCheckFlagOfflineModeEmptyAPIKey(t *testing.T) {
// Passing mock HTTP client, which we expect to be replaced by a NoopClient internally. This test will fail if any calls are made to the mock client.
ctrl := gomock.NewController(t)
mockHTTPClient := mocks.NewMockHTTPClient(ctrl)
client := schematicclient.NewSchematicClient(option.WithAPIKey(""), option.WithHTTPClient(mockHTTPClient))
client.SetFlagDefault("test-flag", true)
defer client.Close()

assert.True(t, client.CheckFlag(context.Background(), &schematicgo.CheckFlagRequestBody{}, "test-flag"))
}

func TestCheckFlagOfflineModeOption(t *testing.T) {
// Passing mock HTTP client, which we expect to be replaced by a NoopClient internally. This test will fail if any calls are made to the mock client.
ctrl := gomock.NewController(t)
mockHTTPClient := mocks.NewMockHTTPClient(ctrl)
client := schematicclient.NewSchematicClient(option.WithOfflineMode(), option.WithHTTPClient(mockHTTPClient))
client.SetFlagDefault("test-flag", true)
defer client.Close()

Expand Down
2 changes: 2 additions & 0 deletions core/custom_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

cache "github.com/schematichq/schematic-go/cache"
"github.com/schematichq/schematic-go/http"
)

// Specify default flag values
Expand Down Expand Up @@ -86,6 +87,7 @@ type ClientOptOfflineMode struct {

func (c ClientOptOfflineMode) applyRequestOptions(opts *RequestOptions) {
opts.OfflineMode = c.isOffline
opts.HTTPClient = http.NewNoopClient()
}

func WithOfflineMode() RequestOption {
Expand Down
4 changes: 4 additions & 0 deletions http/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ func (c NoopClient) Do(*gohttp.Request) (*gohttp.Response, error) {
Body: io.NopCloser(strings.NewReader("")),
}, nil
}

func NewNoopClient() NoopClient {
return NoopClient{}
}

0 comments on commit 3d7dee6

Please sign in to comment.