Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

test_win32inet.py 3.0KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import unittest
  2. import winerror
  3. from pywin32_testutil import str2bytes # py3k-friendly helper
  4. from pywin32_testutil import TestSkipped, testmain
  5. from win32inet import *
  6. from win32inetcon import *
  7. class CookieTests(unittest.TestCase):
  8. def testCookies(self):
  9. data = "TestData=Test"
  10. InternetSetCookie("http://www.python.org", None, data)
  11. got = InternetGetCookie("http://www.python.org", None)
  12. # handle that there might already be cookies for the domain.
  13. bits = map(lambda x: x.strip(), got.split(";"))
  14. self.assertTrue(data in bits)
  15. def testCookiesEmpty(self):
  16. try:
  17. InternetGetCookie("http://site-with-no-cookie.python.org", None)
  18. self.fail("expected win32 exception")
  19. except error as exc:
  20. self.assertEqual(exc.winerror, winerror.ERROR_NO_MORE_ITEMS)
  21. class UrlTests(unittest.TestCase):
  22. def testSimpleCanonicalize(self):
  23. ret = InternetCanonicalizeUrl("foo bar")
  24. self.assertEqual(ret, "foo%20bar")
  25. def testLongCanonicalize(self):
  26. # a 4k URL causes the underlying API to request a bigger buffer"
  27. big = "x" * 2048
  28. ret = InternetCanonicalizeUrl(big + " " + big)
  29. self.assertEqual(ret, big + "%20" + big)
  30. class TestNetwork(unittest.TestCase):
  31. def setUp(self):
  32. self.hi = InternetOpen("test", INTERNET_OPEN_TYPE_DIRECT, None, None, 0)
  33. def tearDown(self):
  34. self.hi.Close()
  35. def testPythonDotOrg(self):
  36. hdl = InternetOpenUrl(
  37. self.hi, "http://www.python.org", None, INTERNET_FLAG_EXISTING_CONNECT
  38. )
  39. chunks = []
  40. while 1:
  41. chunk = InternetReadFile(hdl, 1024)
  42. if not chunk:
  43. break
  44. chunks.append(chunk)
  45. data = str2bytes("").join(chunks)
  46. assert data.find(str2bytes("Python")) > 0, repr(
  47. data
  48. ) # This must appear somewhere on the main page!
  49. def testFtpCommand(self):
  50. # ftp.python.org doesn't exist. ftp.gnu.org is what Python's urllib
  51. # test code uses.
  52. # (As of 2020 it doesn't! Unsurprisingly, it's difficult to find a good
  53. # test server. This test sometimes works, but often doesn't - so handle
  54. # failure here as a "skip")
  55. try:
  56. hcon = InternetConnect(
  57. self.hi,
  58. "ftp.gnu.org",
  59. INTERNET_INVALID_PORT_NUMBER,
  60. None,
  61. None, # username/password
  62. INTERNET_SERVICE_FTP,
  63. 0,
  64. 0,
  65. )
  66. try:
  67. hftp = FtpCommand(hcon, True, FTP_TRANSFER_TYPE_ASCII, "NLST", 0)
  68. try:
  69. print("Connected - response info is", InternetGetLastResponseInfo())
  70. got = InternetReadFile(hftp, 2048)
  71. print("Read", len(got), "bytes")
  72. finally:
  73. hftp.Close()
  74. finally:
  75. hcon.Close()
  76. except error as e:
  77. raise TestSkipped(e)
  78. if __name__ == "__main__":
  79. testmain()