generated from keploy/template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added keploy base file * Added base functions(fetch, test, start, end, put, get) to keploy.py * Added exception handling and fixed bugs in keploy.py * Flask Integration Added * Fixed Denoising Co-authored-by: Mahesh Gupta <maheshg@mindfiresolutions.com>
- Loading branch information
1 parent
26b0be8
commit de5f2bd
Showing
7 changed files
with
162 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from .keploy import Keploy | ||
from .models import * | ||
from .mode import setMode | ||
from .models import Dependency, AppConfig, ServerConfig, FilterConfig, Config, TestCase, TestCaseRequest, TestReq, HttpReq, HttpResp | ||
from .contrib.flask import KFlask | ||
from .mode import setMode, getMode | ||
from .utils import capture_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# BSD 3-Clause License | ||
|
||
import copy | ||
import io | ||
from typing import Any | ||
from flask import Flask, request | ||
from keploy.constants import MODE_OFF | ||
import keploy as k | ||
from keploy.contrib.flask.utils import get_request_data | ||
from keploy.models import HttpResp | ||
from keploy.utils import capture_test | ||
from werkzeug import Response | ||
|
||
class KFlask(object): | ||
|
||
def __init__(self, keploy:k.Keploy=None, app:Flask=None): | ||
self.app = app | ||
self.keploy = keploy | ||
|
||
if not app: | ||
raise ValueError("Flask app instance not passed, Please initiate flask instance and pass it as keyword argument.") | ||
|
||
if not keploy or k.getMode() == MODE_OFF: | ||
return | ||
|
||
app.wsgi_app = KeployMiddleware(keploy, app.wsgi_app) | ||
|
||
|
||
class KeployMiddleware(object): | ||
def __init__(self, kep, app) -> None: | ||
self.app = app | ||
self.keploy = kep | ||
|
||
def __call__(self, environ, start_response) -> Any: | ||
|
||
if not self.keploy: | ||
return self.app(environ, start_response) | ||
|
||
req = {} | ||
res = {} | ||
|
||
def _start_response(status, response_headers, *args): | ||
nonlocal req | ||
nonlocal res | ||
req = get_request_data(request) | ||
res['header'] = {key: [value] for key,value in response_headers} | ||
res['status_code'] = int(status.split(' ')[0]) | ||
return start_response(status, response_headers, *args) | ||
|
||
def _end_response(resp_body): | ||
nonlocal res | ||
res['body'] = b"".join(resp_body).decode("utf8") | ||
return [res['body'].encode('utf-8')] | ||
|
||
resp = _end_response(self.app(environ, _start_response)) | ||
|
||
if environ.get("HTTP_KEPLOY_TEST_ID", None): | ||
self.keploy.put_resp(environ.get('HTTP_KEPLOY_TEST_ID'), HttpResp(**res)) | ||
else: | ||
capture_test(self.keploy, req, res) | ||
|
||
return resp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
def get_request_data(request) -> dict: | ||
req_data = {} | ||
|
||
req_data['header'] = {key: [value] for key,value in request.headers.to_wsgi_list()} | ||
req_data['method'] = request.method | ||
req_data['body'] = request.get_data(as_text=True) | ||
# req_data['form_data'] = request.form.to_dict() | ||
# req_data['file_data'] = { k: v[0].read() for k, v in request.files.lists()} | ||
req_data['uri'] = request.url_rule.rule | ||
req_data['url'] = request.path | ||
req_data['base'] = request.url | ||
req_data['params'] = request.args.to_dict() | ||
|
||
protocol = request.environ.get('SERVER_PROTOCOL', None) | ||
if protocol: | ||
req_data['proto_major'] = int(protocol.split(".")[0][-1]) | ||
req_data['proto_minor'] = int(protocol.split(".")[1]) | ||
|
||
return req_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from keploy.keploy import Keploy | ||
from keploy.models import Dependency, HttpReq, HttpResp, TestCaseRequest | ||
import time | ||
|
||
def capture_test(k, reqst, resp): | ||
|
||
deps = [ Dependency('demo_dep', 'HTTP_CLIENT', {}, None), ] | ||
|
||
test = TestCaseRequest( | ||
captured=int(time.time()), | ||
app_id=k._config.app.name, | ||
uri=reqst['uri'], | ||
http_req=HttpReq( | ||
method=reqst['method'], | ||
proto_major=reqst['proto_major'], | ||
proto_minor=reqst['proto_minor'], | ||
url=reqst['url'], | ||
url_params=reqst['params'], | ||
header=reqst['header'], | ||
body=reqst['body'] | ||
), | ||
http_resp=HttpResp( | ||
status_code=resp['status_code'], | ||
header=resp['header'], | ||
body=resp['body'] | ||
), | ||
deps=deps | ||
) | ||
|
||
k.capture(test) | ||
|
||
return |