rr_test.go

Overview

rr_test.go is a test source file dedicated to verifying the correctness, robustness, and error handling of the HTTP record and replay functionality implemented in the httprr package. This package facilitates recording HTTP client-server interactions to a file and replaying them later for testing or caching purposes. The test file exercises different scenarios including:

The file contains comprehensive test cases validating the integrity and security of the recorded data, and the behavior of the record/replay client under normal and exceptional conditions.


Key Functions and Methods

handler(w http.ResponseWriter, r *http.Request)


always555(w http.ResponseWriter, r *http.Request)


Scrubbing Functions

These functions modify requests or responses to remove or alter sensitive data before recording or replaying.


TestRecordReplay(t *testing.T)


TestErrors(t *testing.T)


Supporting Types and Transports


Important Implementation Details


Interaction with Other Parts of the System


Diagram: Function Interaction and Test Workflow

flowchart TD
subgraph Server Side
handler["handler()"]
always555["always555()"]
end
subgraph Scrubbing Functions
dropPort["dropPort()"]
dropSecretHeader["dropSecretHeader()"]
hideSecretBody["hideSecretBody()"]
doNothing["doNothing()"]
doRefresh["doRefresh()"]
end
subgraph Test Cases
TestRecordReplay["TestRecordReplay()"]
TestErrors["TestErrors()"]
end
subgraph Transports
errTransport["errTransport"]
badRespTransport["badRespTransport"]
end
TestRecordReplay --> |Uses| handler
TestRecordReplay --> |Uses| always555
TestRecordReplay --> |Applies| dropPort
TestRecordReplay --> |Applies| dropSecretHeader
TestRecordReplay --> |Applies| hideSecretBody
TestRecordReplay --> |Applies| doNothing
TestRecordReplay --> |Applies| doRefresh
TestErrors --> |Uses| errTransport
TestErrors --> |Uses| badRespTransport
TestErrors --> |Exercises Errors in| TestRecordReplay

Usage Examples Extracted from TestRecordReplay

rr, err := create(file, http.DefaultTransport)
if err != nil {
    t.Fatal(err)
}
rr, err := Open(file, http.DefaultTransport)
if err != nil {
    t.Fatal(err)
}
rr.ScrubReq(dropPort, dropSecretHeader)
rr.ScrubReq(hideSecretBody)
rr.ScrubResp(doNothing, doRefresh)
req, err := http.NewRequest("GET", srv.URL+"/myrequest", nil)
req.Header.Set("Secret", "key")
resp, err := rr.Client().Do(req)
if err := rr.Close(); err != nil {
    t.Fatal(err)
}

This file serves as a critical validation layer ensuring the HTTP record and replay mechanism works correctly, securely, and resiliently under a variety of normal and error conditions. It demonstrates practical usage patterns and validates the scrubbing capabilities to protect sensitive data during HTTP interaction recording.