You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

75 lines
2.8 KiB

  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using MQTTnet.Implementations;
  3. using System;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace MQTTnet.Tests
  8. {
  9. [TestClass]
  10. public class CrossPlatformSocket_Tests
  11. {
  12. [TestMethod]
  13. public async Task Connect_Send_Receive()
  14. {
  15. var crossPlatformSocket = new CrossPlatformSocket();
  16. await crossPlatformSocket.ConnectAsync("www.google.de", 80, CancellationToken.None);
  17. var requestBuffer = Encoding.UTF8.GetBytes("GET / HTTP/1.1\r\nHost: www.google.de\r\n\r\n");
  18. await crossPlatformSocket.SendAsync(new ArraySegment<byte>(requestBuffer), System.Net.Sockets.SocketFlags.None);
  19. var buffer = new byte[1024];
  20. var length = await crossPlatformSocket.ReceiveAsync(new ArraySegment<byte>(buffer), System.Net.Sockets.SocketFlags.None);
  21. crossPlatformSocket.Dispose();
  22. var responseText = Encoding.UTF8.GetString(buffer, 0, length);
  23. Assert.IsTrue(responseText.Contains("HTTP/1.1 200 OK"));
  24. }
  25. [TestMethod]
  26. public async Task Try_Connect_Invalid_Host()
  27. {
  28. var crossPlatformSocket = new CrossPlatformSocket();
  29. var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(3));
  30. cancellationToken.Token.Register(() => crossPlatformSocket.Dispose());
  31. await crossPlatformSocket.ConnectAsync("www.google.de", 1234, CancellationToken.None);
  32. }
  33. //[TestMethod]
  34. //public async Task Use_Disconnected_Socket()
  35. //{
  36. // var crossPlatformSocket = new CrossPlatformSocket();
  37. // await crossPlatformSocket.ConnectAsync("www.google.de", 80);
  38. // var requestBuffer = Encoding.UTF8.GetBytes("GET /wrong_uri HTTP/1.1\r\nConnection: close\r\n\r\n");
  39. // await crossPlatformSocket.SendAsync(new ArraySegment<byte>(requestBuffer), System.Net.Sockets.SocketFlags.None);
  40. // var buffer = new byte[64000];
  41. // var length = await crossPlatformSocket.ReceiveAsync(new ArraySegment<byte>(buffer), System.Net.Sockets.SocketFlags.None);
  42. // await Task.Delay(500);
  43. // await crossPlatformSocket.SendAsync(new ArraySegment<byte>(requestBuffer), System.Net.Sockets.SocketFlags.None);
  44. //}
  45. [TestMethod]
  46. public void Set_Options()
  47. {
  48. var crossPlatformSocket = new CrossPlatformSocket();
  49. Assert.IsFalse(crossPlatformSocket.ReuseAddress);
  50. crossPlatformSocket.ReuseAddress = true;
  51. Assert.IsTrue(crossPlatformSocket.ReuseAddress);
  52. Assert.IsFalse(crossPlatformSocket.NoDelay);
  53. crossPlatformSocket.NoDelay = true;
  54. Assert.IsTrue(crossPlatformSocket.NoDelay);
  55. }
  56. }
  57. }