Esempio n. 1
0
  def test_complex_with_permutations(self):
    Home = Type("Home",[])
    GetThing = Type("GetThing",[int])
    GetSubThing = Type("GetSubThing", [int,str])
    PostSubThing = Type("PostSubThing", [int])

    parsers = [ 
      p.format( Home, p.s("") ),
      
      p.format( GetThing,  
                p.all_of( [p.method("GET"), p.s("thing"), p.number] ) ),
      
      p.format( GetSubThing,
                p.all_of( [p.method("GET"), p.s("thing"), p.number, p.s("sub"), p.string] ) ),

      p.format( PostSubThing,
                p.all_of( [p.method("POST"), p.s("thing"), p.number, p.s("sub")] ) )
    ]

    # all permutations
    testers = [
      p.parse( identity, p.one_of(parsers_n) ) for parsers_n in permutations(parsers)
    ]
    
    n = 0
    for tester in testers:

      self.assert_success(
        Home(), tester, DummyRequest("GET", "/") )

      self.assert_success(
        GetThing(987), tester, DummyRequest("GET", "/thing/987") )

      self.assert_success(
        GetSubThing(654, "foo"), tester, DummyRequest("GET", "/thing/654/sub/foo") )

      self.assert_success(
        PostSubThing(123), tester, DummyRequest("POST", "/thing/123/sub") )

      self.assert_fail(
        tester, DummyRequest("POST", "/thing/123/") )

      n = n + 1
    
    print "Note: %d permutations tested" % n
Esempio n. 2
0
  def test_one_of(self):
    parsers = [ p.s("one"), p.s("two"), p.s("three") ]
    formatter = Type("Formatter", [])
    
    # all permutations
    testers = [
      p.parse( formatter, p.one_of(parsers_n) ) for parsers_n in permutations(parsers)
    ]

    n = 0
    for tester in testers:

      # success on matching any of the choices
      self.assert_success(
        formatter(), tester, DummyRequest("GET", "/two") )

      self.assert_success(
        formatter(), tester, DummyRequest("GET", "/one") )

      self.assert_success(
        formatter(), tester, DummyRequest("GET", "/three") )
      
      
      # fail on too short
      self.assert_fail(
        tester, DummyRequest("GET", "/") )

      # fail on no parser match
      self.assert_fail(
        tester, DummyRequest("GET", "/four") )

      # fail on too long
      self.assert_fail(
        tester, DummyRequest("GET", "/one/two") )

      # fail on too long and out of order
      self.assert_fail(
        tester, DummyRequest("GET", "/two/one") )

      n = n + 1
    
    print "Note: %d permutations tested" % n
Esempio n. 3
0
  def test_complex(self):
    Home = Type("Home",[])
    GetThing = Type("GetThing",[int])
    PostThing = Type("PostThing",[])
    PutThing = Type("PutThing",[int])
    GetSubThing = Type("GetSubThing", [int,str])
    PostSubThing = Type("PostSubThing", [int])
    PutSubThing = Type("PutSubThing", [int,str])
    Three = Type("Three", [str,int,str])

    parsers = [ 
      p.format( Home, p.s("") ),
      
      p.format( GetThing,  
                p.all_of( [p.method("GET"), p.s("thing"), p.number] ) ),
      
      p.format( PostThing,
                p.all_of( [p.method("POST"), p.s("thing")] ) ),
      
      p.format( PutThing,
                p.all_of( [p.method("PUT"), p.s("thing"), p.number] ) ),
      
      p.format( GetSubThing,
                p.all_of( [p.method("GET"), p.s("thing"), p.number, p.s("sub"), p.string] ) ),

      p.format( PostSubThing,
                p.all_of( [p.method("POST"), p.s("thing"), p.number, p.s("sub")] ) ),
      
      p.format( PutSubThing,
                p.all_of( [p.method("PUT"), p.s("thing"), p.number, p.s("sub"), p.string] ) ),
      
      p.format( Three,
                p.all_of( [p.string, p.number, p.string] ) )
    ]

    tester = p.parse( identity, p.one_of(parsers) )
    
    # success on first matching
    self.assert_success(
      PostSubThing(123), tester, DummyRequest("POST", "/thing/123/sub") )
Esempio n. 4
0
from fungi import mount
from fungi.parse import one_of, all_of, s, format, method, number
from fungi.wsgi import adapter, encode_json, from_html

HomeR = NamedTuple('HomeR', [])
JsonR = NamedTuple('JsonR', [('id', int)])
Routes = Union[HomeR, JsonR]

encode_path = (match(Routes, {
    HomeR: always("/"),
    JsonR: lambda id: "/item/%d" % id
}))

route_parser = (one_of([
    format(HomeR, all_of([method("GET"), s("")])),
    format(JsonR, all_of([method("GET"), s("item"), number]))
]))


def route(req, _):
    return (match(Routes, {
        HomeR: always(render_home(req)),
        JsonR: render_item
    }))


def render_home(req):
    return (resolve("<h1><a href=\"%s\">Hello world!</a></h1>" %
                    encode_path(HomeR())).fmap(from_html))

Esempio n. 5
0
    def app(self, oauth_params):

        Required = Type('Required', [])
        Callback = Type('Callback', [])
        Routes = (Required, Callback)

        encode_path = (match(
            Routes, {
                Required: always("/required"),
                Callback: always("/oauth2callback")
            }))

        def encode_url(route, req):
            return req.relative_url(encode_path(route))

        route_parser = (one_of([
            format(Required, s("required")),
            format(Callback, s("oauth2callback"))
        ]))

        def route(req, _):
            return (match(
                Routes, {
                    Required: always(required_handler(req)),
                    Callback: always(callback_handler(req))
                }))

        def required_handler(req):
            @reject_errors_task
            def _handler(http, reauth, req):
                testcase.assertIsNotNone(http)
                testcase.assertIsNotNone(reauth)
                testcase.was_authorized = True
                return encode_json({'reauth': reauth})

            return _oauth_required(req) >> fapply(_handler)

        def callback_handler(req):
            @reject_errors
            def _spy(respdata):
                testcase.assertResponseRedirect(respdata)
                testcase.assertResponseLocation(encode_url(Required(), req),
                                                respdata)
                key = oauth_params.token_response_param
                if key:
                    testcase.assertResponseLocationParam(
                        key,
                        json.dumps(Http2Fake.content, separators=(',', ':')),
                        respdata)
                testcase.called_callback = True
                return respdata

            return _oauth_callback(req) >> _spy

        def _oauth_required(req):
            return (current_or_redirect_to_login(
                req.url).fmap(lambda u: u.user_id()) >> oauth.authorize(
                    oauth_params, testcase.cache, testcase.secret, req))

        def _oauth_callback(req):
            return (current_or_redirect_to_login(
                req.url).fmap(lambda u: u.user_id()) >> oauth.callback(
                    oauth_params, testcase.cache, testcase.secret, req))

        testcase = self
        testcase.was_authorized = False
        testcase.called_callback = False

        return mount(route_parser, route)
Esempio n. 6
0
File: app.py Progetto: ericgj/fungi
from util.union import match

from fungi import mount
from fungi.parse import one_of, all_of, s, format, method, number
from fungi.gae.memcache import cache_get
from fungi.gae.user import current as current_user, login_url
from fungi.wsgi import adapter, from_html, redirect_to

redirect_to_login = lambda url: login_url(url).fmap(redirect_to)

HomeR = NamedTuple('HomeR', [])
Routes = Union[HomeR]

encode_path = (match(Routes, {HomeR: always("/")}))

route_parser = (one_of([format(HomeR, all_of([method("GET"), s("")]))]))


def route(req, _):
    return (match(Routes, {HomeR: always(render_home(req))}))


def render_home(req):
    def _get_nick(u):
        return cache_get("/".join(('session', u.email(), 'nick')),
                         resolve(u.nickname()))

    def _render(nick):
        return "<h1><a href=\"%s\">Hello, %s!</a></h1>" % (encode_path(
            HomeR()), nick)
Esempio n. 7
0
from fungi.wsgi import adapter, encode_json, from_html

HomeR = NamedTuple('HomeR',[])
JsonR = NamedTuple('JsonR',[('id',int)])
Routes = Union[HomeR,JsonR]    

encode_path = (
  match(Routes, {
    HomeR: always("/"),
    JsonR: lambda id: "/item/%d" % id
  })
)

route_parser = (
  one_of([
    format( HomeR, all_of([ method("GET"), s("") ]) ),
    format( JsonR, all_of([ method("GET"), s("item"), number ]) ) 
  ])
)

def route(req,_):
  return (
    match(Routes, {
      HomeR: always(render_home(req)),
      JsonR: render_item
    })
  )

def render_home(req):
  return (
    resolve(
      "<h1><a href=\"%s\">Hello world!</a></h1>" % encode_path(HomeR())
Esempio n. 8
0
  def app(self, oauth_params):
    
    Required = Type('Required',[])
    Callback = Type('Callback',[])
    Routes = ( Required, Callback )
    
    encode_path = (
      match(Routes, {
        Required: always("/required"),
        Callback: always("/oauth2callback")
      })
    )

    def encode_url(route, req):
      return req.relative_url(encode_path(route))

    route_parser = (
      one_of([
        format( Required, s("required") ),
        format( Callback, s("oauth2callback") )
      ])
    )

    def route(req,_):
      return (
        match(Routes, {
          Required: always(required_handler(req)),
          Callback: always(callback_handler(req)) 
        })
      )

    def required_handler(req):
      @reject_errors_task
      def _handler(http, reauth, req):
        testcase.assertIsNotNone(http)
        testcase.assertIsNotNone(reauth)
        testcase.was_authorized = True
        return encode_json({'reauth': reauth})

      return _oauth_required(req) >> fapply(_handler)

    def callback_handler(req):
      @reject_errors
      def _spy(respdata):
        testcase.assertResponseRedirect(respdata)
        testcase.assertResponseLocation(encode_url(Required(),req), respdata)
        key = oauth_params.token_response_param
        if key:
          testcase.assertResponseLocationParam(
            key, json.dumps(Http2Fake.content, separators=(',',':')), respdata
          )
        testcase.called_callback = True
        return respdata
      return _oauth_callback(req) >> _spy

    def _oauth_required(req):
      return (
        current_or_redirect_to_login(req.url).fmap(lambda u: u.user_id())
          >> oauth.authorize(oauth_params, testcase.cache, testcase.secret, req)
      )
    
    def _oauth_callback(req):
      return (
        current_or_redirect_to_login(req.url).fmap(lambda u: u.user_id())
          >> oauth.callback(oauth_params, testcase.cache, testcase.secret, req)
      )

    testcase = self
    testcase.was_authorized = False
    testcase.called_callback = False
 
    return mount( route_parser, route )
Esempio n. 9
0
HomeR = NamedTuple('HomeR',[])
Routes = Union[HomeR]    

def local_file(fname):
  return os.path.join(os.path.dirname(__file__), fname)

encode_path = (
  match(Routes, {
    HomeR: always("/")
  })
)

route_parser = (
  one_of([
    format( HomeR, all_of([ method("GET"), s("") ]) )
  ])
)

def route(req,config):
  return (
    match(Routes, {
      HomeR: always(render_home(req, config))
    })
  )

def render_home(req,config):
  secret = (
           Right(config['secret']) if config.has_key('secret') 
      else Left(KeyError('No secret found in config')) 
  )