Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CrossPlatformSocket_Tests.cs 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. [ExpectedException(typeof(OperationCanceledException))]
  27. public async Task Try_Connect_Invalid_Host()
  28. {
  29. var crossPlatformSocket = new CrossPlatformSocket();
  30. var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(5));
  31. cancellationToken.Token.Register(() => crossPlatformSocket.Dispose());
  32. await crossPlatformSocket.ConnectAsync("www.google.de", 54321, cancellationToken.Token);
  33. }
  34. //[TestMethod]
  35. //public async Task Use_Disconnected_Socket()
  36. //{
  37. // var crossPlatformSocket = new CrossPlatformSocket();
  38. // await crossPlatformSocket.ConnectAsync("www.google.de", 80);
  39. // var requestBuffer = Encoding.UTF8.GetBytes("GET /wrong_uri HTTP/1.1\r\nConnection: close\r\n\r\n");
  40. // await crossPlatformSocket.SendAsync(new ArraySegment<byte>(requestBuffer), System.Net.Sockets.SocketFlags.None);
  41. // var buffer = new byte[64000];
  42. // var length = await crossPlatformSocket.ReceiveAsync(new ArraySegment<byte>(buffer), System.Net.Sockets.SocketFlags.None);
  43. // await Task.Delay(500);
  44. // await crossPlatformSocket.SendAsync(new ArraySegment<byte>(requestBuffer), System.Net.Sockets.SocketFlags.None);
  45. //}
  46. [TestMethod]
  47. public void Set_Options()
  48. {
  49. var crossPlatformSocket = new CrossPlatformSocket();
  50. Assert.IsFalse(crossPlatformSocket.ReuseAddress);
  51. crossPlatformSocket.ReuseAddress = true;
  52. Assert.IsTrue(crossPlatformSocket.ReuseAddress);
  53. Assert.IsFalse(crossPlatformSocket.NoDelay);
  54. crossPlatformSocket.NoDelay = true;
  55. Assert.IsTrue(crossPlatformSocket.NoDelay);
  56. }
  57. }
  58. }