public HttpResponseMessage Encrypt([FromBody] EnigmaMachineDto enigmaMachineDto, [FromUri] bool leaveWhiteSpace) { HttpResponseMessage response; EnigmaMachine enigmaMachine; try { enigmaMachine = new EnigmaMachine(); enigmaMachine.SetPlugboard(enigmaMachineDto.Plugboard.Wiring); enigmaMachine.ChooseRotors( (RotorNumber)enigmaMachineDto.Rotor1.RotorNum, (RotorNumber)enigmaMachineDto.Rotor2.RotorNum, (RotorNumber)enigmaMachineDto.Rotor3.RotorNum); enigmaMachine.SetRotorDials( enigmaMachineDto.Rotor1.InitialDialSetting, enigmaMachineDto.Rotor2.InitialDialSetting, enigmaMachineDto.Rotor3.InitialDialSetting); enigmaMachine.LeaveWhiteSpace(leaveWhiteSpace); string encodedMessage = enigmaMachine.Encode(enigmaMachineDto.Text); response = Request.CreateResponse(HttpStatusCode.OK, encodedMessage); } catch (Exception ex) { response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex); } return(response); }
public async Task Encrypt_ShouldReturn200OK_WithBasicDto() { // Arrange var controller = new EnigmaController( "https://enigmamachinerestapi.azurewebsites.net/", "api/Enigma/Encrypt?leaveWhiteSpace=true", String.Empty); string textToEncrypt = "I am a jelly doughnut"; EnigmaMachineDto enigmaMachineDto = new EnigmaMachineDto() { MachineName = "foo", Text = textToEncrypt, Rotor1 = new RotorDto() { RotorNum = 1, InitialDialSetting = 'a' }, Rotor2 = new RotorDto() { RotorNum = 2, InitialDialSetting = 'b' }, Rotor3 = new RotorDto() { RotorNum = 3, InitialDialSetting = 'c' }, Plugboard = new PlugboardDto() { Wiring = new Dictionary <char, char>() { { 'a', 'b' }, { 'r', 'e' } } } }; // Act HttpResponseMessage response = await controller.ExecutePostEncrypt <EnigmaMachineDto, HttpResponseMessage>(enigmaMachineDto); string encryptedText = (await response.Content.ReadAsStringAsync()).Replace("\"", ""); // Assert HttpStatusCode statusCode = response.StatusCode; Assert.AreEqual(HttpStatusCode.OK, statusCode); }