public async Task <ConnectionResponse> ReadAsync() { var result = await _client.GetAsync(_uriToCheck); if (result is null) { return(ConnectionResponse.Error( new ResponseError($"{nameof(NetworkStatusConnection)}: result was null!"))); } if (!result.IsSuccessStatusCode) { return(ConnectionResponse.Error( new ResponseError(SensorConnectionConstants.CONNECTION_FAILED_READING))); } if (result.Content is null) { return(ConnectionResponse.Valid(string.Empty)); } var content = await result.Content?.ReadAsByteArrayAsync(); return(ConnectionResponse.Valid(Encoding.ASCII.GetString(content), content)); }
public async Task SensorWithInvalidConnectionShouldReturnInvalidReading() { var networkConnectionMock = new Mock <ISensorConnection>(); networkConnectionMock.Setup(connection => connection.ReadAsync()) .ReturnsAsync(ConnectionResponse.Error( new ResponseError(SensorConnectionConstants.CONNECTION_FAILED_READING))); var sensor = new ConnectionSensor(networkConnectionMock.Object); var reading = await sensor.ReadAsync(); Assert.False(reading.IsValid); }