@@ -27,7 +27,6 @@ import (
2727 "path/filepath"
2828 "strings"
2929 "testing"
30- "time"
3130
3231 "github.com/stretchr/testify/assert"
3332 "github.com/stretchr/testify/require"
@@ -39,24 +38,36 @@ import (
3938 "github.com/elastic/apm-tools/pkg/espoll"
4039)
4140
42- const requestBody = `{"metadata":{"service":{"name":"rum-js-test","agent":{"name":"rum-js","version":"5.5.0"}}}}
41+ func TestRUMXForwardedFor (t * testing.T ) {
42+ systemtest .CleanupElasticsearch (t )
43+
44+ srv := apmservertest .NewUnstartedServerTB (t )
45+ srv .Config .RUM = & apmservertest.RUMConfig {Enabled : true }
46+ err := srv .Start ()
47+ require .NoError (t , err )
48+
49+ serverURL , err := url .Parse (srv .URL )
50+ require .NoError (t , err )
51+ serverURL .Path = "/intake/v2/rum/events"
52+
53+ systemtest .GeoIpLazyDownload (t , serverURL .String ())
54+
55+ const body = `{"metadata":{"service":{"name":"rum-js-test","agent":{"name":"rum-js","version":"5.5.0"}}}}
4356{"transaction":{"trace_id":"611f4fa950f04631aaaaaaaaaaaaaaaa","id":"611f4fa950f04631","type":"page-load","duration":643,"span_count":{"started":0}}}
4457{"metricset":{"samples":{"transaction.breakdown.count":{"value":12},"transaction.duration.sum.us":{"value":12},"transaction.duration.count":{"value":2},"transaction.self_time.sum.us":{"value":10},"transaction.self_time.count":{"value":2},"span.self_time.count":{"value":1},"span.self_time.sum.us":{"value":633.288}},"transaction":{"type":"request","name":"GET /"},"span":{"type":"external","subtype":"http"},"timestamp": 1496170422281000}}
4558`
4659
47- func lazyDownload (t * testing.T , url string ) ([]espoll.SearchHit , bool ) {
48- t .Helper ()
49-
50- systemtest .CleanupElasticsearch (t )
51-
52- req , err := http .NewRequest ("POST" , url , strings .NewReader (requestBody ))
60+ req , err := http .NewRequest ("POST" , serverURL .String (), strings .NewReader (body ))
5361 require .NoError (t , err )
5462
5563 req .Header .Set ("Content-Type" , "application/x-ndjson" )
5664 req .Header .Set ("X-Forwarded-For" , "220.244.41.16" )
5765
5866 resp , err := http .DefaultClient .Do (req )
5967 require .NoError (t , err )
68+
69+ _ , err = io .ReadAll (resp .Body )
70+ require .NoError (t , err )
6071 require .Equal (t , http .StatusAccepted , resp .StatusCode )
6172
6273 io .Copy (io .Discard , resp .Body )
@@ -75,48 +86,12 @@ func lazyDownload(t *testing.T, url string) ([]espoll.SearchHit, bool) {
7586 Field : "processor.event" ,
7687 Values : []any {"transaction" , "metric" },
7788 },
78- espoll .WithTimeout (30 * time .Second ),
79- )
80-
81- for _ , hit := range result .Hits .Hits {
82- tags , ok := hit .Source ["tags" ]
83- if ok {
84- t .Logf ("unexpect tags field: %v" , tags )
85- return nil , false
86- }
87- }
88-
89- return result .Hits .Hits , true
90- }
91-
92- func TestRUMXForwardedFor (t * testing.T ) {
93- systemtest .CleanupElasticsearch (t )
94-
95- srv := apmservertest .NewUnstartedServerTB (t )
96- srv .Config .RUM = & apmservertest.RUMConfig {Enabled : true }
97- err := srv .Start ()
98- require .NoError (t , err )
99-
100- serverURL , err := url .Parse (srv .URL )
101- require .NoError (t , err )
102- serverURL .Path = "/intake/v2/rum/events"
103-
104- var (
105- ok bool
106- hits []espoll.SearchHit
107- )
108- assert .Eventually (t ,
109- func () bool {
110- hits , ok = lazyDownload (t , serverURL .String ())
111- return ok
112- },
113- 3 * time .Minute ,
114- 30 * time .Second ,
115- "failed to lazily download geoip database" ,
89+ // espoll.WithTimeout(30*time.Second),
11690 )
11791
92+ // Also checks for the absence of `tags` fields.
11893 approvaltest .ApproveFields (
119- t , t .Name (), hits ,
94+ t , t .Name (), result . Hits . Hits ,
12095 // RUM timestamps are set by the server based on the time the payload is received.
12196 "@timestamp" , "timestamp.us" ,
12297 // RUM events have the source port recorded, and in the tests it will be dynamic
0 commit comments