|
| 1 | +package dsl |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "net/url" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/google/go-cmp/cmp" |
| 13 | + "github.com/ooni/probe-engine/pkg/model" |
| 14 | + "github.com/ooni/probe-engine/pkg/runtimex" |
| 15 | +) |
| 16 | + |
| 17 | +// TestMeasurexliteHTTPIncludeResponseBodySnapshot checks whether we include or not |
| 18 | +// include a body snapshot into the JSON measurement depending on the settings. |
| 19 | +func TestMeasurexliteHTTPIncludeResponseBodySnapshot(t *testing.T) { |
| 20 | + // define the expected response body |
| 21 | + expectedBody := []byte("Bonsoir, Elliot!\r\n") |
| 22 | + |
| 23 | + // create local test server |
| 24 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 25 | + w.Write(expectedBody) |
| 26 | + })) |
| 27 | + defer server.Close() |
| 28 | + |
| 29 | + // parse the server's URL |
| 30 | + URL := runtimex.Try1(url.Parse(server.URL)) |
| 31 | + |
| 32 | + // define the function to create a measurement pipeline |
| 33 | + makePipeline := func(options ...HTTPTransactionOption) Stage[*Void, *HTTPResponse] { |
| 34 | + return Compose4( |
| 35 | + NewEndpoint(URL.Host, NewEndpointOptionDomain("example.com")), |
| 36 | + TCPConnect(), |
| 37 | + HTTPConnectionTCP(), |
| 38 | + HTTPTransaction(options...), |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + // define the function to return the body in the pipeline |
| 43 | + getPipelineBody := func(input Maybe[*HTTPResponse]) ([]byte, error) { |
| 44 | + // handle the case of unexpected error |
| 45 | + if input.Error != nil { |
| 46 | + return nil, input.Error |
| 47 | + } |
| 48 | + return input.Value.ResponseBodySnapshot, nil |
| 49 | + } |
| 50 | + |
| 51 | + // define the function to return the body in the measurement |
| 52 | + getMeasurementBody := func(observations *Observations) ([]byte, error) { |
| 53 | + if len(observations.Requests) != 1 { |
| 54 | + return nil, errors.New("expected a single request entry") |
| 55 | + } |
| 56 | + return []byte(observations.Requests[0].Response.Body.Value), nil |
| 57 | + } |
| 58 | + |
| 59 | + // define the function to run the measurement |
| 60 | + measure := func(options ...HTTPTransactionOption) (Maybe[*HTTPResponse], *Observations) { |
| 61 | + pipeline := makePipeline(options...) |
| 62 | + rtx := NewMeasurexliteRuntime(model.DiscardLogger, &NullMetrics{}, time.Now()) |
| 63 | + input := NewValue(&Void{}) |
| 64 | + output := pipeline.Run(context.Background(), rtx, input) |
| 65 | + observations := ReduceObservations(rtx.ExtractObservations()...) |
| 66 | + return output, observations |
| 67 | + } |
| 68 | + |
| 69 | + t.Run("the default should be that of not including the body", func(t *testing.T) { |
| 70 | + output, observations := measure( /* empty */ ) |
| 71 | + |
| 72 | + t.Run("the pipeline body should contain the body", func(t *testing.T) { |
| 73 | + pipeBody, err := getPipelineBody(output) |
| 74 | + if err != nil { |
| 75 | + t.Fatal(err) |
| 76 | + } |
| 77 | + t.Log("pipeline body", pipeBody) |
| 78 | + if diff := cmp.Diff(expectedBody, pipeBody); diff != "" { |
| 79 | + t.Fatal(diff) |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("the measurement body should be empty", func(t *testing.T) { |
| 84 | + measBody, err := getMeasurementBody(observations) |
| 85 | + if err != nil { |
| 86 | + t.Fatal(err) |
| 87 | + } |
| 88 | + t.Log("measurement body", measBody) |
| 89 | + if len(measBody) != 0 { |
| 90 | + t.Fatal("expected empty body") |
| 91 | + } |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("but we can optionally request to include it", func(t *testing.T) { |
| 96 | + output, observations := measure(HTTPTransactionOptionIncludeResponseBodySnapshot(true)) |
| 97 | + |
| 98 | + t.Run("the pipeline body should contain the body", func(t *testing.T) { |
| 99 | + pipeBody, err := getPipelineBody(output) |
| 100 | + if err != nil { |
| 101 | + t.Fatal(err) |
| 102 | + } |
| 103 | + t.Log("pipeline body", pipeBody) |
| 104 | + if diff := cmp.Diff(expectedBody, pipeBody); diff != "" { |
| 105 | + t.Fatal(diff) |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + t.Run("the measurement body should contain the body", func(t *testing.T) { |
| 110 | + measBody, err := getMeasurementBody(observations) |
| 111 | + if err != nil { |
| 112 | + t.Fatal(err) |
| 113 | + } |
| 114 | + t.Log("measurement body", measBody) |
| 115 | + if diff := cmp.Diff(expectedBody, measBody); diff != "" { |
| 116 | + t.Fatal(diff) |
| 117 | + } |
| 118 | + }) |
| 119 | + }) |
| 120 | +} |
0 commit comments