Parse incoming requests and add tests.

This commit is contained in:
Elliot Glaysher 2018-11-15 13:43:10 -08:00
parent 98228adcbf
commit 7f64ac3b1f
2 changed files with 108 additions and 10 deletions

View File

@ -22,7 +22,7 @@
:: +note: private request from light to another vane
::
+$ note
$% :: %b:
$% :: %b: to behn
::
$: %b
::
@ -273,16 +273,38 @@
|%
:: +parse-channel-request: parses a list of channel-requests
::
:: TODO: All of it.
:: Parses a json array into a list of +channel-request. If any of the items
:: in the list fail to parse, the entire thing fails so we can 400 properly
:: to the client.
::
++ parse-channel-request
|= =json
^- (list channel-request)
:: %- ar:dejs-soft:format
:: |= =^json
:: ?. ?=([%o *] json)
:: ~
!!
|= request-list=json
^- (unit (list channel-request))
:: parse top
::
=, dejs-soft:format
=- ((ar -) request-list)
::
|= item=json
^- (unit channel-request)
::
?~ maybe-key=((ot action+so ~) item)
~
?: =('ack' u.maybe-key)
((pe %ack (ot id+ni ~)) item)
?: =('poke' u.maybe-key)
((pe %poke (ot ship+(su fed:ag) app+so mark+so json+some ~)) item)
?: =('subscribe' u.maybe-key)
%. item
%+ pe %subscribe
(ot ship+(su fed:ag) app+so path+(su ;~(pfix fas (more fas urs:ab))) ~)
?: =('unsubscribe' u.maybe-key)
%. item
%+ pe %unsubscribe
(ot ship+(su fed:ag) app+so path+(su ;~(pfix fas (more fas urs:ab))) ~)
:: if we reached this, we have an invalid action key. fail parsing.
::
~
:: +file-not-found-page: 404 page for when all other options failed
::
++ file-not-found-page
@ -1090,7 +1112,6 @@
:: %init: tells us what our ship name is
::
%init
~& %light-born
::
=. ship.ax [~ our.task]
:: initial value for the login handler

View File

@ -688,6 +688,83 @@
!> (rush '192.168.1.1' simplified-url-parser:light-gate)
==
::
++ test-parse-channel-request
;: weld
%+ expect-eq
!> `[%ack 5]~
!> %- parse-channel-request:light-gate
(need (de-json:html '[{"action": "ack", "id": 5}]'))
::
%+ expect-eq
!> `[%poke ~nec %app1 %app-type [%n '5']]~
!> %- parse-channel-request:light-gate
%- need %- de-json:html
'''
[{"action": "poke",
"ship": "nec",
"app": "app1",
"mark": "app-type",
"json": 5}]
'''
::
%+ expect-eq
!> `[%subscribe ~sampyl-sipnym %hall /this/path]~
!> %- parse-channel-request:light-gate
%- need %- de-json:html
'''
[{"action": "subscribe",
"ship": "sampyl-sipnym",
"app": "hall",
"path": "/this/path"}]
'''
::
%+ expect-eq
!> `[%unsubscribe ~marlyt %thing /other]~
!> %- parse-channel-request:light-gate
%- need %- de-json:html
'''
[{"action": "unsubscribe",
"ship": "marlyt",
"app": "thing",
"path": "/other"}]
'''
::
:: after lunch, check error conditions and multipart requests.
%+ expect-eq
!> ~
!> %- parse-channel-request:light-gate
%- need %- de-json:html
'[{"noaction": "noaction"}]'
::
%+ expect-eq
!> ~
!> %- parse-channel-request:light-gate
%- need %- de-json:html
'[{"action": "bad-action"}]'
::
%+ expect-eq
!> ~
!> %- parse-channel-request:light-gate
%- need %- de-json:html
'[{"action": "ack", "id": 5}, {"action": "bad-action"}]'
::
%+ expect-eq
!> :- ~
:~ [%ack 9]
[%poke ~bud %wut %wut-type [%a [%n '2'] [%n '1'] ~]]
==
!> %- parse-channel-request:light-gate
%- need %- de-json:html
'''
[{"action": "ack", "id": 9},
{"action": "poke",
"ship": "bud",
"app": "wut",
"mark": "wut-type",
"json": [2, 1]}]
'''
==
::
++ light-call
|= $: light-gate=_light-gate
now=@da