summaryrefslogtreecommitdiff
path: root/libs/jsonrpc/tests/Response/ResponseBuilderTest.php
blob: e2dcb2a0c8e95d09662659e0bbf449a45ba38c41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php

use JsonRPC\Exception\AccessDeniedException;
use JsonRPC\Exception\AuthenticationFailureException;
use JsonRPC\Exception\InvalidJsonFormatException;
use JsonRPC\Exception\InvalidJsonRpcFormatException;
use JsonRPC\Exception\ResponseEncodingFailureException;
use JsonRPC\Exception\ResponseException;
use JsonRPC\Response\ResponseBuilder;

require_once __DIR__.'/../../../../vendor/autoload.php';

class ResponseBuilderTest extends PHPUnit_Framework_TestCase
{
    public function testBuildResponse()
    {
        $response = ResponseBuilder::create()
            ->withId(123)
            ->withResult('test')
            ->build();

        $this->assertEquals('{"jsonrpc":"2.0","result":"test","id":123}', $response);
    }

    public function testBuildResponseWithError()
    {
        $response = ResponseBuilder::create()
            ->withId(123)
            ->withResult('test')
            ->withError(42, 'Test', 'More info')
            ->build();

        $this->assertEquals('{"jsonrpc":"2.0","error":{"code":42,"message":"Test","data":"More info"},"id":123}', $response);
    }

    public function testBuildResponseWithException()
    {
        $response = ResponseBuilder::create()
            ->withId(123)
            ->withResult('test')
            ->withException(new Exception('Test'))
            ->build();

        $this->assertEquals('{"jsonrpc":"2.0","error":{"code":0,"message":"Test"},"id":123}', $response);
    }

    public function testBuildResponseWithResponseException()
    {
        $exception = new ResponseException('Error', 42);
        $exception->setData('Data');

        $response = ResponseBuilder::create()
            ->withId(123)
            ->withResult('test')
            ->withException($exception)
            ->build();

        $this->assertEquals('{"jsonrpc":"2.0","error":{"code":42,"message":"Error","data":"Data"},"id":123}', $response);
    }

    public function testBuildResponseWithAccessDeniedException()
    {
        $responseBuilder = ResponseBuilder::create();
        $response = $responseBuilder
            ->withId(123)
            ->withResult('test')
            ->withException(new AccessDeniedException('Test'))
            ->build();

        $this->assertEquals('{"jsonrpc":"2.0","error":{"code":403,"message":"Forbidden"},"id":123}', $response);
        $this->assertEquals('HTTP/1.0 403 Forbidden', $responseBuilder->getStatus());

        $this->assertEquals(
            array('Content-Type' => 'application/json'),
            $responseBuilder->getHeaders()
        );
    }

    public function testBuildResponseWithAuthenticationFailureException()
    {
        $responseBuilder = ResponseBuilder::create();
        $response = $responseBuilder
            ->withId(123)
            ->withResult('test')
            ->withException(new AuthenticationFailureException('Test'))
            ->build();

        $this->assertEquals('{"jsonrpc":"2.0","error":{"code":401,"message":"Unauthorized"},"id":123}', $response);
        $this->assertEquals('HTTP/1.0 401 Unauthorized', $responseBuilder->getStatus());

        $this->assertEquals(
            array('Content-Type' => 'application/json', 'WWW-Authenticate' => 'Basic realm="JsonRPC"'),
            $responseBuilder->getHeaders()
        );
    }

    public function testBuildResponseWithResponseEncodingFailureException()
    {
        $response = ResponseBuilder::create()
            ->withId(123)
            ->withResult('test')
            ->withException(new ResponseEncodingFailureException('Test'))
            ->build();

        $this->assertEquals('{"jsonrpc":"2.0","error":{"code":-32603,"message":"Internal error","data":"Test"},"id":123}', $response);
    }

    public function testBuildResponseWithInvalidArgumentException()
    {
        $response = ResponseBuilder::create()
            ->withId(123)
            ->withResult('test')
            ->withException(new InvalidArgumentException('Test'))
            ->build();

        $this->assertEquals('{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params","data":"Test"},"id":123}', $response);
    }

    public function testBuildResponseWithBadFunctionCallException()
    {
        $response = ResponseBuilder::create()
            ->withId(123)
            ->withResult('test')
            ->withException(new BadFunctionCallException('Test'))
            ->build();

        $this->assertEquals('{"jsonrpc":"2.0","error":{"code":-32601,"message":"Method not found"},"id":123}', $response);
    }

    public function testBuildResponseWithInvalidJsonRpcFormatException()
    {
        $response = ResponseBuilder::create()
            ->withId(123)
            ->withResult('test')
            ->withException(new InvalidJsonRpcFormatException('Test'))
            ->build();

        $this->assertEquals('{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid Request"},"id":null}', $response);
    }

    public function testBuildResponseWithInvalidJsonFormatException()
    {
        $response = ResponseBuilder::create()
            ->withId(123)
            ->withResult('test')
            ->withException(new InvalidJsonFormatException('Test'))
            ->build();

        $this->assertEquals('{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":null}', $response);
    }
}