@@ -55,6 +55,50 @@ func TestHandlerForAllowingHealthcheck(t *testing.T) {
5555 }
5656}
5757
58+ func TestHandlerWithMetadata (t * testing.T ) {
59+ csp := CSPReport {
60+ CSPReportBody {
61+ DocumentURI : "http://example.com" ,
62+ BlockedURI : "http://example.com" ,
63+ },
64+ }
65+
66+ payload , _ := json .Marshal (csp )
67+
68+ for _ , repeats := range []int {1 , 2 } {
69+ var logBuffer bytes.Buffer
70+ log .SetOutput (& logBuffer )
71+
72+ url := "/?"
73+ for i := 0 ; i < repeats ; i ++ {
74+ url += fmt .Sprintf ("metadata=value%d&" , i )
75+ }
76+
77+ request , err := http .NewRequest ("POST" , url , bytes .NewBuffer (payload ))
78+ if err != nil {
79+ t .Fatalf ("failed to create request: %v" , err )
80+ }
81+ recorder := httptest .NewRecorder ()
82+
83+ handleViolationReport (recorder , request )
84+
85+ response := recorder .Result ()
86+ defer response .Body .Close ()
87+
88+ if response .StatusCode != http .StatusOK {
89+ t .Errorf ("expected HTTP status %v; got %v" , http .StatusOK , response .StatusCode )
90+ }
91+
92+ log := logBuffer .String ()
93+ if ! strings .Contains (log , "metadata=value0" ) {
94+ t .Fatalf ("Logged result should contain metadata value0 in '%s'" , log )
95+ }
96+ if strings .Contains (log , "metadata=value1" ) {
97+ t .Fatalf ("Logged result shouldn't contain metadata value1 in '%s'" , log )
98+ }
99+ }
100+ }
101+
58102func TestValidateViolationWithInvalidBlockedURIs (t * testing.T ) {
59103 invalidBlockedURIs := []string {
60104 "resource://" ,
0 commit comments