FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
testcontainers-python/modules/nginx/example_basic.py at main · f18m/testcontainers-python · GitHub
f18m
/
testcontainers-python
Public
forked from
testcontainers/testcontainers-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
testcontainers-python
/
modules
/
nginx
/
example_basic.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
116 lines (93 loc) · 3.52 KB
Breadcrumbs
testcontainers-python
/
modules
/
nginx
/
example_basic.py
Copy path
File metadata and controls
116 lines (93 loc) · 3.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import
json
import
os
from
pathlib
import
Path
import
requests
from
testcontainers
.
nginx
import
NginxContainer
def
basic_example
():
with
NginxContainer
()
as
nginx
:
# Get connection parameters
host
=
nginx
.
get_container_host_ip
()
port
=
nginx
.
get_exposed_port
(
nginx
.
port
)
nginx_url
=
f"http://
{
host
}
:
{
port
}
"
print
(
f"Nginx URL:
{
nginx_url
}
"
)
# Create test HTML file
test_html
=
"""
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<h1>Hello from Nginx!</h1>
<p>This is a test page.</p>
</body>
</html>
"""
# Create test directory and file
test_dir
=
Path
(
"/tmp/nginx_test"
)
test_dir
.
mkdir
(
exist_ok
=
True
)
test_file
=
test_dir
/
"index.html"
test_file
.
write_text
(
test_html
)
# Copy test file to container
nginx
.
get_container
().
copy_to_container
(
test_file
,
"/usr/share/nginx/html/"
)
print
(
"Copied test file to container"
)
# Test basic HTTP request
response
=
requests
.
get
(
nginx_url
)
print
(
f"
\n
Basic request status:
{
response
.
status_code
}
"
)
print
(
f"Content type:
{
response
.
headers
.
get
(
'content-type'
)
}
"
)
print
(
f"Content length:
{
response
.
headers
.
get
(
'content-length'
)
}
"
)
# Test HEAD request
head_response
=
requests
.
head
(
nginx_url
)
print
(
"
\n
HEAD request headers:"
)
print
(
json
.
dumps
(
dict
(
head_response
.
headers
),
indent
=
2
))
# Create test configuration
test_config
=
"""
server {
listen 80;
server_name test.local;
location /test {
return 200 'Test location';
}
location /redirect {
return 301 /test;
}
location /error {
return 404 'Not Found';
}
}
"""
# Write and copy configuration
config_file
=
test_dir
/
"test.conf"
config_file
.
write_text
(
test_config
)
nginx
.
get_container
().
copy_to_container
(
config_file
,
"/etc/nginx/conf.d/"
)
print
(
"
\n
Copied test configuration"
)
# Reload Nginx configuration
nginx
.
get_container
().
exec_run
(
"nginx -s reload"
)
print
(
"Reloaded Nginx configuration"
)
# Test custom location
test_response
=
requests
.
get
(
f"
{
nginx_url
}
/test"
)
print
(
f"
\n
Test location response:
{
test_response
.
text
}
"
)
# Test redirect
redirect_response
=
requests
.
get
(
f"
{
nginx_url
}
/redirect"
,
allow_redirects
=
False
)
print
(
f"
\n
Redirect status:
{
redirect_response
.
status_code
}
"
)
print
(
f"Redirect location:
{
redirect_response
.
headers
.
get
(
'location'
)
}
"
)
# Test error
error_response
=
requests
.
get
(
f"
{
nginx_url
}
/error"
)
print
(
f"
\n
Error status:
{
error_response
.
status_code
}
"
)
print
(
f"Error response:
{
error_response
.
text
}
"
)
# Get Nginx version
version_response
=
requests
.
get
(
nginx_url
)
server
=
version_response
.
headers
.
get
(
"server"
)
print
(
f"
\n
Nginx version:
{
server
}
"
)
# Test with different HTTP methods
methods
=
[
"GET"
,
"POST"
,
"PUT"
,
"DELETE"
,
"OPTIONS"
]
print
(
"
\n
HTTP method tests:"
)
for
method
in
methods
:
response
=
requests
.
request
(
method
,
nginx_url
)
print
(
f"
{
method
}
:
{
response
.
status_code
}
"
)
# Clean up
os
.
remove
(
test_file
)
os
.
remove
(
config_file
)
os
.
rmdir
(
test_dir
)
if
__name__
==
"__main__"
:
basic_example
()
Back
|
FazBrowse Home
|
New Git URL