Ejemplo n.º 1
0
def make_response(text: str, encoding: str, body_encoding: str) -> Response:
    return Response.from_dict({
        "body": text.encode(body_encoding),
        "type": "json",
        "encoding": encoding,
        "headers": {
            "content-type": "application/json"
        },
        "url": "http://test",
        "status_code": 200,
        "elapsed": datetime.timedelta(seconds=1),
        "elapsed_sec": 1.0,
    })
Ejemplo n.º 2
0
def make_response(text: str, encoding: str, body_encoding: str) -> Response:
    return Response.from_dict({
        "body": text.encode(body_encoding),
        "type": "json",
        "encoding": encoding,
        "headers": {
            "content-type": "application/json"
        },
        "url": "http://test",
        "status_code": 200,
        "elapsed": datetime.timedelta(seconds=1),
        "elapsed_sec": 1.0,
    })
Ejemplo n.º 3
0
def make_response(type: str, status_code: int) -> Response:
    return Response.from_dict({
        "body": b'{"body": true}',
        "type": type,
        "encoding": "utf-8",
        "headers": {
            "content-type": "application/json"
        },
        "url": "http://test",
        "status_code": status_code,
        "elapsed": datetime.timedelta(seconds=1),
        "elapsed_sec": 1.0,
    })
Ejemplo n.º 4
0
def make_response(type: str, status_code: int) -> Response:
    return Response.from_dict({
        "body": b'{"body": true}',
        "type": type,
        "encoding": "utf-8",
        "headers": {
            "content-type": "application/json"
        },
        "url": "http://test",
        "status_code": status_code,
        "elapsed": datetime.timedelta(seconds=1),
        "elapsed_sec": 1.0,
    })
Ejemplo n.º 5
0
def apply_first_condition(res: Response, req: Request, conditions: TList[Condition]) -> Response:
    condition: TOption[Condition] = conditions.find(
        lambda c: when_optional_filter(c.when, {'req': req.to_dict(), 'res': res.to_dict()})
    )
    if condition.is_none():
        return res

    return Response.from_dict(
        {
            "body": res.body,
            "type": condition.get().type,
            "encoding": res.encoding.get(),
            "headers": res.headers,
            "url": res.url,
            "status_code": res.status_code,
            "elapsed": res.elapsed,
            "elapsed_sec": res.elapsed_sec,
        },
    )
Ejemplo n.º 6
0
def apply_first_condition(res: Response, req: Request,
                          conditions: TList[Condition]) -> Response:
    # TODO: remove TOption (owlmixin... find)
    condition: TOption[Condition] = TOption(
        conditions.find(lambda c: when_optional_filter(c.when, {
            'req': req.to_dict(),
            'res': res.to_dict()
        })))
    if condition.is_none():
        return res

    return Response.from_dict(
        {
            "body": res.body,
            "type": condition.get().type,
            "encoding": res.encoding.get(),
            "headers": res.headers,
            "url": res.url,
            "status_code": res.status_code,
            "elapsed": res.elapsed,
            "elapsed_sec": res.elapsed_sec,
        }, )
Ejemplo n.º 7
0
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import datetime
import pytest
from owlmixin.util import load_yaml

from jumeaux.addons.judgement.same import Executor
from jumeaux.models import JudgementAddOnPayload, Response, CaseInsensitiveDict, JudgementAddOnReference

EMPTY_KEYS = {'changed': [], 'added': [], 'removed': []}

RES_ONE = Response.from_dict({
    'body': b'a',
    "type": "unknown",
    'headers': CaseInsensitiveDict({}),
    'url': 'url',
    'status_code': 200,
    'elapsed': datetime.timedelta(seconds=1),
    "elapsed_sec": 1.0,
})

RES_OTHER = Response.from_dict({
    'body': b'b',
    "type": "unknown",
    'headers': CaseInsensitiveDict({}),
    'url': 'url',
    'status_code': 200,
    'elapsed': datetime.timedelta(seconds=2),
    "elapsed_sec": 2.0,
})
        {
            'title': 'Check point 2',
            'conditions': [
                {
                    'added': ['<add><99>']
                }
            ]
        }
    ]
}

RES_ONE = Response.from_dict({
    'body': b'a',
    "type": "unknown",
    'headers': CaseInsensitiveDict({}),
    'url': 'url',
    'status_code': 200,
    'elapsed': datetime.timedelta(seconds=1),
    "elapsed_sec": 1.0,
})

RES_OTHER = Response.from_dict({
    'body': b'b',
    "type": "unknown",
    'headers': CaseInsensitiveDict({}),
    'url': 'url',
    'status_code': 200,
    'elapsed': datetime.timedelta(seconds=2),
    "elapsed_sec": 2.0,
})
Ejemplo n.º 9
0
        <author>次郎</author>
        <title>次郎22</title>
    </book>
</catalog>
"""

NORMAL_CASE = ("Normal",
               """
               force: False 
               """,
               Response.from_dict({
                   "body": NORMAL_BODY.encode('euc-jp'),
                   "type": "xml",
                   "encoding": 'euc-jp',
                   "headers": {
                       "content-type": "application/xml"
                   },
                   "url": "http://test",
                   "status_code": 200,
                   "elapsed": datetime.timedelta(seconds=1),
                   "elapsed_sec": 1.0,
               }),
               {
                   "catalog": {
                       "book": [
                           {"@id": "bk001", "author": "Ichiro", "title": "Ichiro55"},
                           {"@id": "bk002", "author": "次郎", "title": "次郎22"}
                       ]
                   }
               }
               )
Ejemplo n.º 10
0
  <div id="main">
    <span>Main contents</span>
  </div>
</body>
</html>
"""

NORMAL_CASE = ("Normal", """
               force: False
               """,
               Response.from_dict({
                   "body": NORMAL_BODY.encode('utf8'),
                   "type": "html",
                   "encoding": 'utf8',
                   "headers": {
                       "content-type": "text/html"
                   },
                   "url": "http://test",
                   "status_code": 200,
                   "elapsed": datetime.timedelta(seconds=1),
                   "elapsed_sec": 1.0,
               }), {
                   "html": {
                       "head": {
                           "title": {
                               "##value": "タイトル"
                           },
                           "meta": [{
                               "#name": "format-detection",
                               "#content": "telephone=no",
                               "##value": ""
                           }, {
Ejemplo n.º 11
0
        }]
    },
    ensure_ascii=False)

CORRUPTION_BODY_BYTES: bytes = '{"normal": "次郎", '.encode(
    'euc-jp') + '"corruption": "三郎"}'.encode('utf8')

NORMAL_CASE = ("Normal", """
               force: False 
               """,
               Response.from_dict({
                   "body": NORMAL_BODY.encode('euc-jp'),
                   "type": "json",
                   "encoding": 'euc-jp',
                   "headers": {
                       "content-type": "application/json; charset=euc-jp"
                   },
                   "url": "http://test",
                   "status_code": 200,
                   "elapsed": datetime.timedelta(seconds=1),
                   "elapsed_sec": 1.0,
               }), NORMAL_BODY.encode('euc-jp'), 'euc-jp', """
{
    "items": [
        {
            "favorites": [
                "apple",
                "orange"
            ],
            "id": 1,
            "name": "Ichiro"
        },
Ejemplo n.º 12
0
""".replace(os.linesep, '').replace('  ', '')

CORRUPTION_BODY_BYTES: bytes = \
    '<!DOCTYPE html><html><head><title>タイトル</title></head>'.encode('utf8') + \
    '<body><div>コンテンツ</div></body></html>'.encode('euc-jp')

NORMAL_CASE = ("Normal",
               """
               force: False 
               """,
               Response.from_dict({
                   "body": NORMAL_BODY.encode('euc-jp'),
                   "type": "html",
                   "encoding": 'euc-jp',
                   "headers": {
                       "content-type": "text/html; charset=euc-jp"
                   },
                   "url": "http://test",
                   "status_code": 200,
                   "elapsed": datetime.timedelta(seconds=1),
                   "elapsed_sec": 1.0,
               }),
               NORMAL_BODY.encode('euc-jp'),
               'euc-jp',
               """<html>
 <head>
  <title>
   タイトル
  </title>
  <meta content="telephone=no" name="format-detection"/>
  <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
  <script charset="utf-8" type="text/javascript">
Ejemplo n.º 13
0
Name: Jumeaux Viewer
Version: 1.0.0 (r1585)
""".strip()

PATTERN1 = ("Normal",
            """
            force: False 
            header_regexp: '\\[(.+)\\]'
            record_regexp: '([^:]+): (.+)'
            """,
            Response.from_dict({
                "body": PATTERN1_BODY.encode('utf-8'),
                "type": "plain",
                "encoding": 'utf-8',
                "headers": {
                    "content-type": "text/plain; charset=utf-8"
                },
                "url": "http://test",
                "status_code": 200,
                "elapsed": datetime.timedelta(seconds=1),
                "elapsed_sec": 1.0,
            }),
            {
                "Module1": {
                    "Name": "Jumeaux",
                    "License": "MIT",
                    "Version": "0.33.0"
                },
                "Module2 alpha": {
                    "Name": "Jumeaux Viewer",
                    "Version": "1.0.0 (r: 1585:1586)"
                }
Ejemplo n.º 14
0
    <book id="bk002">
        <author>次郎</author>
        <title>次郎22</title>
    </book>
</catalog>
"""

NORMAL_CASE = ("Normal", """
               force: False 
               """,
               Response.from_dict({
                   "body": NORMAL_BODY.encode('euc-jp'),
                   "type": "xml",
                   "encoding": 'euc-jp',
                   "headers": {
                       "content-type": "application/xml"
                   },
                   "url": "http://test",
                   "status_code": 200,
                   "elapsed": datetime.timedelta(seconds=1),
                   "elapsed_sec": 1.0,
               }), {
                   "catalog": {
                       "book": [{
                           "@id": "bk001",
                           "author": "Ichiro",
                           "title": "Ichiro55"
                       }, {
                           "@id": "bk002",
                           "author": "次郎",
                           "title": "次郎22"
                       }]
Ejemplo n.º 15
0
[Module2 alpha]
Name: Jumeaux Viewer
Version: 1.0.0 (r1585)
""".strip()

PATTERN1 = ("Normal", """
            force: False 
            header_regexp: '\\[(.+)\\]'
            record_regexp: '([^:]+): (.+)'
            """,
            Response.from_dict({
                "body": PATTERN1_BODY.encode('utf-8'),
                "type": "plain",
                "encoding": 'utf-8',
                "headers": {
                    "content-type": "text/plain; charset=utf-8"
                },
                "url": "http://test",
                "status_code": 200,
                "elapsed": datetime.timedelta(seconds=1),
                "elapsed_sec": 1.0,
            }), {
                "Module1": {
                    "Name": "Jumeaux",
                    "License": "MIT",
                    "Version": "0.33.0"
                },
                "Module2 alpha": {
                    "Name": "Jumeaux Viewer",
                    "Version": "1.0.0 (r: 1585:1586)"
                }
            })
Ejemplo n.º 16
0
    <span>Main contents</span>
  </div>
</body>
</html>
"""

NORMAL_CASE = ("Normal",
               """
               force: False
               """,
               Response.from_dict({
                   "body": NORMAL_BODY.encode('utf8'),
                   "type": "html",
                   "encoding": 'utf8',
                   "headers": {
                       "content-type": "text/html"
                   },
                   "url": "http://test",
                   "status_code": 200,
                   "elapsed": datetime.timedelta(seconds=1),
                   "elapsed_sec": 1.0,
               }),
               {
                   "html": {
                       "head": {
                           "title": {
                               "##value": "タイトル"
                           },
                           "meta": [
                               {
                                   "#name": "format-detection",
                                   "#content": "telephone=no",