Skip to content

Commit 385e515

Browse files
committed
tests
1 parent 0a74764 commit 385e515

File tree

4 files changed

+64
-55
lines changed

4 files changed

+64
-55
lines changed

cloudapp/app/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def echo_html():
7878

7979
@app.route('/foo/', methods=['GET'])
8080
def ex_test():
81-
return jsonify({'info': 'bar'})
81+
return jsonify({"info": { "foo": True}})
8282

8383
return app
8484

labapp/app/app.py

+4-50
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import requests
99
import markdown
1010
from ce import get_ce_info, get_ce_state
11+
from fetch import cloudapp_fetch
1112

1213
app = Flask(__name__)
1314
app.config['ce_info'] = None
@@ -43,53 +44,6 @@ def eph_ns() -> str:
4344
this_eph_ns = request.cookies.get('eph_ns', None)
4445
return this_eph_ns
4546

46-
def cloudapp_fetch(session, url, timeout, prop, value, headers = {}):
47-
"""
48-
Fetch data from URL
49-
Validate prop and value in the JSON response
50-
"""
51-
response = session.get(url, timeout=timeout)
52-
response.raise_for_status()
53-
data = response.json()
54-
if data.get(prop) != value:
55-
raise ValueError(f"Invalid {prop}: expected {value}, got {data.get(prop)}")
56-
if data.get("request_headers"):
57-
clean_headers = headers_cleaner(data['request_headers'])
58-
data['request_headers'] = clean_headers
59-
return data
60-
return data
61-
62-
def cloudapp_fetch2(session, url, timeout, prop, key, value):
63-
"""
64-
Fetch data from URL
65-
Validate if a specific key-value pair is present in the dictionary located at `prop` in the JSON response
66-
"""
67-
response = session.get(url, timeout=timeout)
68-
response.raise_for_status()
69-
data = response.json()
70-
71-
prop_data = data.get(prop, {})
72-
if not isinstance(prop_data, dict) or prop_data.get(key) != value:
73-
raise ValueError(f"Expected {key}: {value} in {prop}, but got {key}: {prop_data.get(key)}")
74-
75-
if data.get("request_headers"):
76-
clean_headers = headers_cleaner(data['request_headers'])
77-
data['request_headers'] = clean_headers
78-
79-
return data
80-
81-
def headers_cleaner(headers):
82-
"""
83-
Remove headers that contain specific substrings.
84-
Use this to make responses look nicer.
85-
"""
86-
unwanted_substrings = ['x-envoy', 'cloudfront', 'x-k8se']
87-
filtered_headers = {
88-
key: value for key, value in headers.items()
89-
if not any(substring in key.lower() for substring in unwanted_substrings)
90-
}
91-
return filtered_headers
92-
9347
@app.errorhandler(404)
9448
@app.errorhandler(500)
9549
def return_err(err):
@@ -248,7 +202,7 @@ def ex_test():
248202
s = requests.Session()
249203
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
250204
url = f"https://foo.{app.config['base_url']}/"
251-
data = cloudapp_fetch2(s, url, 5, 'info', 'bar')
205+
data = cloudapp_fetch(s, url, 7, 'info', {"foo": True})
252206
return jsonify(status='success', data=data)
253207
except (LabException, requests.RequestException, ValueError) as e:
254208
return jsonify(status='fail', error=str(e))
@@ -260,7 +214,7 @@ def ex_test2():
260214
s = requests.Session()
261215
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
262216
url = f"https://bar.{app.config['base_url']}/"
263-
data = cloudapp_fetch(s, url, 5, 'info', 'foo')
217+
data = cloudapp_fetch(s, url, 7, 'info', {"bar": True})
264218
return jsonify(status='success', data=data)
265219
except (LabException, requests.RequestException, ValueError) as e:
266220
return jsonify(status='fail', error=str(e))
@@ -276,7 +230,7 @@ def lb_aws():
276230
if not ns:
277231
raise LabException("Ephemeral NS not set")
278232
url = f"https://{ns}.{app.config['base_url']}"
279-
data = cloudapp_fetch(s, url, 5, 'env', 'AWS')
233+
data = cloudapp_fetch(s, url, 7, 'env', 'AWS', None)
280234
return jsonify(status='success', data=data)
281235
except (LabException, requests.RequestException, ValueError) as e:
282236
return jsonify(status='fail', error=str(e))

labapp/app/fetch.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
def headers_cleaner(headers):
2+
"""
3+
Remove headers that contain specific substrings.
4+
Use this to make responses look nicer.
5+
"""
6+
unwanted_substrings = ['x-envoy', 'cloudfront', 'x-k8se']
7+
filtered_headers = {
8+
key: value for key, value in headers.items()
9+
if not any(substring in key.lower() for substring in unwanted_substrings)
10+
}
11+
return filtered_headers
12+
13+
def cloudapp_fetch(session, url, timeout, prop, value):
14+
"""
15+
Fetch data from URL
16+
Validate prop and value in the JSON response
17+
"""
18+
response = session.get(url, timeout=timeout)
19+
response.raise_for_status()
20+
data = response.json()
21+
print(data)
22+
if data.get(prop) != value:
23+
raise ValueError(f'Invalid {prop}: expected {value}, got {data.get(prop)}')
24+
if data.get("request_headers"):
25+
clean_headers = headers_cleaner(data['request_headers'])
26+
data['request_headers'] = clean_headers
27+
return data
28+
return data
29+
30+
def cloudapp_fetch_new(session, url, timeout, prop, key, value):
31+
"""
32+
Fetch data from URL
33+
Validate if a specific key-value pair is present in the dictionary located at `prop` in the JSON response
34+
"""
35+
response = session.get(url, timeout=timeout)
36+
response.raise_for_status()
37+
38+
print(response.text)
39+
data = response.json()
40+
41+
print(data)
42+
43+
prop_data = data.get(prop, {})
44+
if not isinstance(prop_data, dict) or prop_data.get(key) != value:
45+
raise ValueError(f"Expected {key}: {value} in {prop}, but got {dict}")
46+
47+
if data.get("request_headers"):
48+
clean_headers = headers_cleaner(data['request_headers'])
49+
data['request_headers'] = clean_headers
50+
51+
return data

labapp/app/markdown/overview.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ Here are some examples to try.
7474
GET https://foo.f5demos.com/ HTTP/1.1
7575
7676
{
77-
"info": "bar"
77+
"info": {
78+
"foo": True
79+
}
7880
}
7981
```
8082

@@ -89,15 +91,17 @@ document.getElementById('requestBtn1').addEventListener('click', () => {
8991
</script>
9092

9193
The test made a request to <strong>https://foo.f5demos.com</strong>.
92-
The test succeeded because the response contained the ``JSON`` string ``{ "info": "bar" }``.
94+
The test succeeded because the response contained the ``JSON`` string ``{ "info": { "foo": True }}``.
9395

9496
<div style="height:25px"></div>
9597

9698
```http
9799
GET https://bar.f5demos.com/ HTTP/1.1
98100
99101
{
100-
"info": "foo"
102+
"info": {
103+
"bar": True
104+
}
101105
}
102106
```
103107

@@ -112,7 +116,7 @@ document.getElementById('requestBtn2').addEventListener('click', () => {
112116
</script>
113117

114118
The test made a request to <strong>https://bar.f5demos.com</strong>.
115-
The test failed because the response did not contain the ``JSON`` string ``{ "info": "bar" }``.
119+
The test failed because the response did not contain the ``JSON`` string ``{ "info": { "bar": True}}``.
116120

117121

118122
<div style="height:25px"></div>

0 commit comments

Comments
 (0)