test_url.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #
  2. # Copyright 2018-2022 Elyra Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from pathlib import Path
  17. from requests import session
  18. from elyra.util.url import FileTransportAdapter
  19. def test_valid_file_url():
  20. """
  21. Verify that the FileTransportAdapter works as expected for valid input
  22. """
  23. requests_session = session()
  24. requests_session.mount("file://", FileTransportAdapter())
  25. # utilize the test source code as resource
  26. this_file = __file__
  27. url = Path(this_file).as_uri()
  28. res = requests_session.get(url)
  29. assert res.status_code == 200, this_file
  30. with open(this_file, "r") as source:
  31. assert res.text == source.read()
  32. def test_invalid_file_url():
  33. """
  34. Verify that the FileTransportAdapter works as expected for error
  35. scenarios:
  36. - requested resource is a directory
  37. - requested resource not found
  38. - request method is not 'GET'
  39. """
  40. requests_session = session()
  41. requests_session.mount("file://", FileTransportAdapter())
  42. # utilize the test source code as resource
  43. this_file_p = Path(__file__)
  44. # requested resource is a directory
  45. url = this_file_p.parent.as_uri()
  46. res = requests_session.get(url)
  47. assert res.status_code == 400, url
  48. assert res.reason == "Not a file"
  49. assert len(res.text) == 0
  50. # requested resource wasn't found
  51. url = this_file_p.resolve().with_name("no-such-file").as_uri()
  52. res = requests_session.get(url)
  53. assert res.status_code == 404, url
  54. assert res.reason == "File not found"
  55. assert len(res.text) == 0
  56. # request method is not supported
  57. url = this_file_p.as_uri()
  58. unsupported_methods = [
  59. requests_session.delete,
  60. requests_session.head,
  61. requests_session.options,
  62. requests_session.patch,
  63. requests_session.post,
  64. requests_session.put,
  65. ]
  66. for unsupported_method in unsupported_methods:
  67. res = unsupported_method(url)
  68. assert res.status_code == 405, url
  69. assert res.reason == "Method not allowed"