Code Duplication    Length = 11-13 lines in 5 locations

tests/CsrfHeaderCheckMiddlewareTest.php 5 locations

@@ 28-40 (lines=13) @@
25
        $this->assertSame('foobar', (string) $response->getBody());
26
    }
27
28
    public function testFailingPostRequestNoHost()
29
    {
30
        $request = new ServerRequest([], [], "http://alice.com/hello", "Post");
31
        $request = $request->withHeader('Origin', "http://alice.com");
32
        $request = $request->withoutHeader('Host');
33
34
        $middleware = CsrfHeaderCheckMiddlewareFactory::createDefault();
35
36
        $this->expectException(CsrfHeaderCheckMiddlewareException::class);
37
        $this->expectExceptionMessage('Could not find the HOST header in the HTTP request.');
38
39
        $response = $middleware->process($request, $this->getDelegate());
40
    }
41
42
    public function testSuccessfullPostWithOriginAndHost()
43
    {
@@ 42-52 (lines=11) @@
39
        $response = $middleware->process($request, $this->getDelegate());
40
    }
41
42
    public function testSuccessfullPostWithOriginAndHost()
43
    {
44
        $request = new ServerRequest([], [], "http://alice.com/hello", "Post");
45
        $request = $request->withHeader('Origin', "http://alice.com");
46
47
        $middleware = CsrfHeaderCheckMiddlewareFactory::createDefault();
48
49
        $response = $middleware->process($request, $this->getDelegate());
50
51
        $this->assertSame('foobar', (string) $response->getBody());
52
    }
53
54
    public function testSuccessfullPostWithOriginAndHostAndPort()
55
    {
@@ 54-64 (lines=11) @@
51
        $this->assertSame('foobar', (string) $response->getBody());
52
    }
53
54
    public function testSuccessfullPostWithOriginAndHostAndPort()
55
    {
56
        $request = new ServerRequest([], [], "http://alice.com:8080/hello", "Post");
57
        $request = $request->withHeader('Origin', "http://alice.com:8080");
58
59
        $middleware = CsrfHeaderCheckMiddlewareFactory::createDefault();
60
61
        $response = $middleware->process($request, $this->getDelegate());
62
63
        $this->assertSame('foobar', (string) $response->getBody());
64
    }
65
66
    public function testAttackPostWithOriginAndHost()
67
    {
@@ 66-76 (lines=11) @@
63
        $this->assertSame('foobar', (string) $response->getBody());
64
    }
65
66
    public function testAttackPostWithOriginAndHost()
67
    {
68
        $request = new ServerRequest([], [], "http://alice.com/hello", "Post");
69
        $request = $request->withHeader('Origin', "http://eve.com");
70
71
        $middleware = CsrfHeaderCheckMiddlewareFactory::createDefault();
72
73
        $this->expectException(CsrfHeaderCheckMiddlewareException::class);
74
        $this->expectExceptionMessage('Potential CSRF attack stopped. Source origin and target origin do not match.');
75
        $response = $middleware->process($request, $this->getDelegate());
76
    }
77
78
    public function testExceptionOnWeirdRequests()
79
    {
@@ 78-89 (lines=12) @@
75
        $response = $middleware->process($request, $this->getDelegate());
76
    }
77
78
    public function testExceptionOnWeirdRequests()
79
    {
80
        $request = new ServerRequest([], [], "http://alice.com/hello", "Post");
81
        $request = $request->withHeader('Origin', "http://eve.com");
82
        $request = $request->withAddedHeader('Origin', "http://alice.com");
83
84
        $middleware = CsrfHeaderCheckMiddlewareFactory::createDefault();
85
86
        $this->expectException(CsrfHeaderCheckMiddlewareException::class);
87
        $this->expectExceptionMessage('Unexpected request: more than one ORIGIN header sent.');
88
        $response = $middleware->process($request, $this->getDelegate());
89
    }
90
}
91