# README
HTTP Replay
Synopsis
// install the hook for HTTP replaying:
if h, ok := client.HTTPClient.(*http.Client); ok {
_, err := httpreplay.InstallRecorder(h)
if err != nil {
return err
}
}
func TestMyServiceResource_basic(t *testing.T) {
// In a unit test, tell the recorder what test we are running
httpreplay.SetScenario("TestMyServiceResource_basic")
defer httpreplay.SaveScenario()
... testing happens ...
}
Description
This library provides a recording mechanism for the OCI-GO-SDK. It hooks into the 'Transport' layer of the HTTP request, calling through to the real network as needed.
- bypass (default): Do nothing
- record: Store the Interaction
- replay: Load the Interaction file and send back the response
Select the record or replay mode by specifying a build tag to go: -tags <mode>
Functions
InstallRecorder
- Install hooks into the
http.Client
to allow the record/replay library to intercept HTTP calls.InstallRecorder
tries to be safe if called multiple times, but it is possible to fool it. Best is to only call it once perhttp.Client
.
SetScenario
-
Name the scenario that is about to run.
Currently, this specifies the filename that the scenario data will be saved into, with a
.yaml
extension.- If
-tags record
is specified, the requests are written to this file in theSaveScenario
call. The Interaction file will be stored in directory~<prj_path>/record/
with the name passed into. - If
-tags replay
is specified, then a file by that name is immediately read and used for generating replies to network requests.
- If
SaveScenario
-
Save the scenario data.
Currently, if
-tags record
is specified, this writes all the recorded requests to the file named inSetScenario
.
Record Storage
- In record mode: After running the test case, the record file will be stored under "oci/record/".
- In replay mode: Look for the record file under "oci/record/" and throw error if it is not found.
Example usage
- To run normally:
go test
- Or run 1 specific test case:
go test -run <testname>
- To record interactions:
go test -tags record
- Or to record 1 specific test case:
go test -run <testname> -tags record
- To replay interactions:
go test -tags replay
- Or to replay 1 specific test case:
go test -run <testname> -tags replay
Example Output
Run with recording turned on, the test portion takes 2411 seconds:
> go test -v -timeout 120m -run TestResourceCoreImageTestSuite -tags record
=== RUN TestResourceCoreImageTestSuite
=== RUN TestResourceCoreImageTestSuite/TestAccResourceCoreImage_basic
=== RUN TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageTuple
=== RUN TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageUri
--- PASS: TestResourceCoreImageTestSuite (2411.14s)
--- PASS: TestResourceCoreImageTestSuite/TestAccResourceCoreImage_basic (2410.58s)
--- SKIP: TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageTuple (0.00s)
core_image_resource_test.go:191: Long running test, requires per tenancy namespace + bucket + image export object to run
--- SKIP: TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageUri (0.00s)
core_image_resource_test.go:155: Long running test, requires exported image available via public url
PASS
Now that we have a recording, run in replay mode, note that it is only 4.09 seconds:
> go test -v -run TestResourceCoreImageTestSuite -tags replay
=== RUN TestResourceCoreImageTestSuite
=== RUN TestResourceCoreImageTestSuite/TestAccResourceCoreImage_basic
=== RUN TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageTuple
=== RUN TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageUri
--- PASS: TestResourceCoreImageTestSuite (4.09s)
--- PASS: TestResourceCoreImageTestSuite/TestAccResourceCoreImage_basic (3.60s)
--- SKIP: TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageTuple (0.00s)
core_image_resource_test.go:191: Long running test, requires per tenancy namespace + bucket + image export object to run
--- SKIP: TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageUri (0.00s)
core_image_resource_test.go:155: Long running test, requires exported image available via public url
PASS
# Functions
No description provided by the author
InstallRecorder does no-op.
No description provided by the author
Load reads a scenario file from disk.
ModeRecordReplay returns true in record and replay.
NewRecorder creates a new recorder.
NewRecorderAsMode creates a new recorder in the specified mode.
NewScenario creates a new empty Scenario.
SaveScenario saves the recorded service calls for the current scenario.
No description provided by the author
SetScenario lets the recorder know which test is currently executing.
ShouldRetryImmediately returns true if replaying.
# Variables
ErrInteractionNotFound indicates that a requested interaction was not found in the scenario file.
# Structs
Interaction type contains a pair of request/response for a single HTTP interaction between a client and a server.
Recorder represents a type used to record and replay client and server interactions.
Request represents a client request as recorded in the scenario file.
Response represents a server response as recorded in the scenario file.
Scenario type.
# Interfaces
HTTPRecordingClient wraps the execution of a http request, adding record/replay functionality.
# Type aliases
No description provided by the author
Matcher function returns true when the actual request matches a single HTTP interaction's request according to the function's own criteria.
Mode represents recording/playback mode.
Transformer converts a request and a saved interaction into a result.