Пример #1
0
        public void ErrorMessage_ShouldSerializeThenDeserializeBackToEqualErrorMessage()
        {
            // Arrange
            var error = new JsonError(null, new JsonErrorObject(-32600, "Invalid Request"));

            // Act
            var json   = JsonConvert.SerializeObject(error);
            var result = JsonConvert.DeserializeObject <JsonError>(json);

            // Assert
            Assert.That(result, Is.EqualTo(error));
        }
Пример #2
0
        public void ErrorMessage_ShouldSerializeTwiceToSameJson()
        {
            // Arrange
            var error = new JsonError(null, new JsonErrorObject(-32600, "Invalid Request"));

            // Act
            var json1 = JsonConvert.SerializeObject(error);
            var json2 = JsonConvert.SerializeObject(JsonConvert.DeserializeObject <JsonError>(json1));

            // Assert
            Assert.That(json2, Is.EqualTo(json1));
        }
        public void GivenInvalidJson_ReturnsAParseError()
        {
            // Arrange
            var json       = "{jsonrpc: '2.0', method: 'foobar, 'params': 'bar', 'baz]";
            var rpcService = CreateNew();

            // Act
            string output = rpcService.Process(json);

            // Assert
            var result   = JsonConvert.DeserializeObject <JsonResponse>(output);
            var expected = new JsonError(null, new JsonErrorObject(-32700, "Parse error"));

            Assert.That(result, Is.EqualTo(expected).Using <JsonResponse>(new JsonResponseComparer()));
        }
        public void GivenAnInvalidRequest_ReturnsAnInvalidRequestError()
        {
            // Arrange
            var json       = "1";
            var rpcService = CreateNew();

            // Act
            string output = rpcService.Process(json);

            // Assert
            var result   = JsonConvert.DeserializeObject <JsonResponse>(output);
            var expected = new JsonError(null, new JsonErrorObject(-32600, "Invalid request"));

            Assert.That(result, Is.EqualTo(expected).Using <JsonResponse>(new JsonResponseComparer()));
        }
        /// <inheritdoc />
        public override bool Equals(JsonResponse x, JsonResponse y)
        {
            JsonError xerr = x as JsonError;
            JsonError yerr = y as JsonError;

            if (xerr != null && yerr != null)
            {
                return(Object.Equals(xerr.Id, yerr.Id) &&
                       xerr.Error.Code == yerr.Error.Code);
            }
            else
            {
                return(Object.Equals(x, y));
            }
        }
        public void GivenInvalidBatchJson_ReturnsAParseError()
        {
            // Arrange
            var json       = @"[
                { 'jsonrpc': '2.0', 'method': 'sum', 'params': [1,2,4], 'id': '1'},
                {'jsonrpc': '2.0', 'method'
            ]";
            var rpcService = CreateNew();

            // Act
            string output = rpcService.Process(json);

            // Assert
            var result   = JsonConvert.DeserializeObject <JsonResponse>(output);
            var expected = new JsonError(null, new JsonErrorObject(-32700, "Parse error"));

            Assert.That(result, Is.EqualTo(expected).Using <JsonResponse>(new JsonResponseComparer()));
        }
        public void RequestForUnknownMethod_ReturnsMethodNotFoundError()
        {
            // Arrange
            var json       = @"{
                jsonrpc: '2.0',
                method: 'foobar_foobaz',
                id: 1
            }";
            var rpcService = CreateNew();

            // Act
            string output = rpcService.Process(json);

            // Assert
            var result   = JsonConvert.DeserializeObject <JsonResponse>(output);
            var expected = new JsonError("1", new JsonErrorObject(-32601, "Method not found"));

            Assert.That(result, Is.EqualTo(expected).Using <JsonResponse>(new JsonResponseComparer()));
        }
        /// <inheritdoc />
        public override int GetHashCode(JsonResponse obj)
        {
            JsonError err = obj as JsonError;

            if (err != null)
            {
                int hash = 17;
                unchecked
                {
                    hash = hash * 29 + err.Id?.GetHashCode() ?? 0;
                    hash = hash * 29 + err.Error.Code.GetHashCode();
                }
                return(hash);
            }
            else
            {
                return(obj?.GetHashCode() ?? 0);
            }
        }
Пример #9
0
        public void ErrorMessage_ShouldDeserializeToAJsonError()
        {
            // Arrange
            string json = @"{
                jsonrpc: '2.0',
                error: {
                    code: -32600,
                    message: 'Invalid Request'
                },
                id: null
            }";

            // Act
            var result = JsonConvert.DeserializeObject <JsonError>(json);

            // Assert
            var expected = new JsonError(null, new JsonErrorObject(-32600, "Invalid Request"));

            Assert.That(result, Is.EqualTo(expected));
        }
Пример #10
0
 /// <inheritdoc />
 public bool Equals(JsonError other)
 {
     return(base.Equals(other) &&
            this.Error.Equals(other.Error));
 }