'Test POST filter interpretation with JSON request body'

def testPOSTFilterAjax(self):

    assertEqual = self.assertEqual
    assertNotIn = self.assertNotIn
    request = current.request
    jsonstr = '{"service_organisation.service_id__belongs":"1","~.example__lt":1,"~.other__like":[1,2]}'
    request._body = StringIO(jsonstr)
    r = S3Request(prefix='org', name='organisation', http='POST', get_vars={'$search': 'ajax', 'test': 'retained'})
    assertEqual(r.http, 'GET')
    get_vars = r.get_vars
    assertNotIn('$search', get_vars)
    assertEqual(get_vars.get('service_organisation.service_id__belongs'), '1')
    assertEqual(get_vars.get('~.example__lt'), '1')
    assertEqual(get_vars.get('~.other__like'), ['1', '2'])
    assertEqual(get_vars.get('test'), 'retained')
    request._body = StringIO('{"service_organisation.service_id__belongs":"1"}')
    r = S3Request(prefix='org', name='organisation', http='POST', get_vars={'test': 'retained'})
    assertEqual(r.http, 'POST')
    get_vars = r.get_vars
    assertNotIn('$search', get_vars)
    assertNotIn('service_organisation.service_id__belongs', get_vars)
    assertEqual(get_vars.get('test'), 'retained')
    request._body = StringIO('[1,2,3]')
    r = S3Request(prefix='org', name='organisation', http='POST', get_vars={'$search': 'ajax', 'test': 'retained'})
    assertEqual(r.http, 'GET')
    get_vars = r.get_vars
    assertNotIn('$search', get_vars)
    assertNotIn('service_organisation.service_id__belongs', get_vars)
    assertEqual(get_vars.get('test'), 'retained')
    request._body = StringIO('')
    r = S3Request(prefix='org', name='organisation', http='POST', get_vars={'$search': 'ajax', 'test': 'retained'})
    assertEqual(r.http, 'GET')
    get_vars = r.get_vars
    assertNotIn('$search', get_vars)
    assertNotIn('service_organisation.service_id__belongs', get_vars)
    assertEqual(get_vars.get('test'), 'retained')
