Skip to content

Commit 4750e40

Browse files
[py] Add test for File Upload
1 parent 5ebc0e6 commit 4750e40

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

py/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ py_library(
143143
data = [
144144
"pytest.ini",
145145
"setup.cfg",
146+
"test/selenium/webdriver/common/test_file.txt"
146147
],
147148
imports = ["."],
148149
deps = [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lorem ipsum dolor sit amet
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Licensed to the Software Freedom Conservancy (SFC) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The SFC licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
from selenium.webdriver.common.by import By
19+
20+
21+
import os
22+
23+
24+
def test_can_upload_file(driver, pages):
25+
26+
pages.load("upload.html")
27+
current_dir = os.path.dirname(os.path.realpath(__file__))
28+
driver.find_element(By.ID, 'upload').send_keys(os.path.join(current_dir, "test_file.txt"))
29+
driver.find_element(By.ID, 'go').click()
30+
driver.switch_to.frame(driver.find_element(By.ID, "upload_target"))
31+
body = driver.find_element(By.CSS_SELECTOR, "body").text
32+
33+
assert "test_file.txt" in body

py/test/selenium/webdriver/common/webserver.py

+42
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import logging
2222
import os
23+
import re
2324
import socket
2425
import threading
2526
from io import open
@@ -76,6 +77,47 @@ def do_GET(self):
7677
except IOError:
7778
self.send_error(404, 'File Not Found: %s' % path)
7879

80+
def do_POST(self):
81+
"""POST method handler."""
82+
try:
83+
remaining_bytes = int(self.headers['content-length'])
84+
contents = ""
85+
line = self.rfile.readline()
86+
contents += line.decode("utf-8")
87+
remaining_bytes -= len(line)
88+
line = self.rfile.readline()
89+
contents += line.decode("utf-8")
90+
remaining_bytes -= len(line)
91+
fn = re.findall(r'Content-Disposition.*name="upload"; filename="(.*)"', line.decode("utf-8"))
92+
if not fn:
93+
self.send_error(500, f"File not found. {contents}")
94+
return
95+
line = self.rfile.readline()
96+
remaining_bytes -= len(line)
97+
contents += line.decode("utf-8")
98+
line = self.rfile.readline()
99+
remaining_bytes -= len(line)
100+
contents += line.decode("utf-8")
101+
preline = self.rfile.readline()
102+
remaining_bytes -= len(preline)
103+
while remaining_bytes > 0:
104+
line = self.rfile.readline()
105+
remaining_bytes -= len(line)
106+
contents += line.decode("utf-8")
107+
108+
self.send_response(200)
109+
self.send_header('Content-type', 'text/html')
110+
self.end_headers()
111+
112+
self.wfile.write(
113+
f"""<!doctype html>
114+
{contents}
115+
<script>window.top.window.onUploadDone();</script>
116+
""".encode('utf-8')
117+
)
118+
except Exception as e:
119+
self.send_error(500, f"Error found: {e}")
120+
79121
def log_message(self, format, *args):
80122
"""Override default to avoid trashing stderr"""
81123
pass

0 commit comments

Comments
 (0)