Changeset 21032 in project


Ignore:
Timestamp:
10/25/10 20:58:48 (8 years ago)
Author:
certainty
Message:

ext-direct: declared test-dependency to make automated testing work.

Location:
release/4/ext-direct/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • release/4/ext-direct/trunk/examples/polling/polling.html

    r19624 r21032  
    1010    <script language="javascript" src="http://localhost:8080/polling.js"></script>
    1111    <script type="text/javascript" src="http://localhost:8080/api"></script>
     12    <script type="text/javascript">
     13        Ext.Direct.addProvider(Ext.app.REMOTING_API);
     14    </script>
    1215
    1316  </head>
  • release/4/ext-direct/trunk/examples/polling/polling.js

    r19667 r21032  
    77 */
    88Ext.onReady(function(){
     9/*
    910    Ext.Direct.addProvider(
    1011        Ext.app.REMOTING_API,
     
    1415        }
    1516    );
     17*/
    1618
    1719    var out = new Ext.form.DisplayField({
  • release/4/ext-direct/trunk/examples/polling/polling.scm

    r19667 r21032  
    33;;
    44;; Created: So Aug 22 15:14:48 2010 (CEST)
    5 ;; Last-Updated: Mo Aug 23 21:11:37 2010 (CEST)
     5;; Last-Updated: Sa Sep  4 09:11:00 2010 (CEST)
    66;;           By:
    77
     
    1010(require-extension awful spiffy)
    1111(load "../../ext-direct")
     12(load "../../ext-direct-awful-integration")
    1213
    1314(import awful spiffy)
    14 
    1515
    1616(define (define-json path body)
     
    2626(define (my-handler arg1) "[]")
    2727
    28 
    2928;;we expose a single handler
    3029(api `((TestAction ((doEcho ,my-handler) (multiply ,my-handler)))))
     
    3231(define-json "api"
    3332  (lambda () (api->json "/rpc")))
     33
     34
     35(define-page "rpc"
     36  (lambda ()
     37    "Bogus"))
  • release/4/ext-direct/trunk/ext-direct.meta

    r19451 r21032  
    66 (hidden)
    77 (doc-from-wiki)
     8 (test-depends test)
    89 (depends defstruct awful intarweb)
    910 (files "ext-direct.setup" "ext-direct.scm"))
  • release/4/ext-direct/trunk/ext-direct.scm

    r19668 r21032  
    33;;
    44;; Created: So Aug 15 11:42:55 2010 (CEST)
    5 ;; Last-Updated: Mo Aug 23 21:39:26 2010 (CEST)
     5;; Last-Updated: Sa Sep  4 09:47:33 2010 (CEST)
    66;;           By:
    77
     
    2020;;
    2121(define api (make-parameter '()))
     22(define api-variable (make-parameter "Ext.app.REMOTING_API"))
     23
     24;; will be set during integration or by procedures the user supplies
     25(define reader    (make-parameter #f))
     26(define responder (make-parameter #f))
     27(define current-ext-direct-request (make-parameter #f))
     28
    2229
    2330(defstruct ext-direct-request type tid method data action upload arguments)
    2431(defstruct ext-direct-response type tid action method result success)
    2532
    26 (define current-ext-direct-request (make-parameter #f))
    2733
    2834;; utilities for requests/responses
     
    5258(define (form-request? request) #f)
    5359
     60
    5461;; This is the main-interface to the stack
    55 ;; It expects a request-reader and a responder
     62;; It expects a reader and a responder
    5663;; See awful-responder and awful-request-reader for an example
    57 (define (process-request reader responder)
    58   (receive (is-form-request requests) (reader)
     64(define (process-request #!key (ext-reader (reader)) (ext-responder (responder)))
     65 
     66  (unless (procedure? ext-reader) (error "Ext-direct reader is not defined. Did you integrate?"))
     67  (unless (procedure? ext-responder) (error "Ext-direct responder is not defined. Did you integrate?"))
     68 
     69  (receive (is-form-request requests) (ext-reader)
    5970    (for-each (lambda (response)
    60                 (responder response is-form-request))
     71                (ext-responder response is-form-request))
    6172              (run-requests requests))))
    6273
     
    6475(define (read-form-request request) #f)
    6576
     77;; Read the requests from the current http-request. Due to
     78;; request-batching this procedure returns a list of
     79;; ext-direct-requests. Request must be a valid intarweb-request
     80
    6681(define (read-json-request request)
    6782  (let* ((json (json-read (request-port request))))
    6883    (if (list? json) (map json->request json) (list (json->request json)))))
    6984
     85
     86;; A single http-request can represent multiple Ext-direct
     87;; requests. The frontend is free to do request-batching. So that's
     88;; why this procedure accepts multiple requests
    7089(define (run-requests requests)
    7190  (map safe-run-request requests))
    7291
     92
     93;; runs the request and turns any error-conditions
     94;; into a suitable error-response
    7395(define (safe-run-request request)
    7496 (condition-case (run-request request)
    7597                 [var () (make-error-response request)]))
    7698
     99;; create a response that represents an error
     100;; With this type of error-response the frontend can gracefully handle
     101;; serverside errors
    77102(define (make-error-response request)
    78103  (make-ext-direct-response tid: (ext-direct-request-tid request)  type: "exception" success: #f  errors: (list)))
     
    90115
    91116
    92 ;; ok now let's export the api
     117;; Encode the defined api into the needed javascript
     118;; to export the server-side api to the frontend
    93119(define (api->json router-path)
    94120  (define (procedure-arity proc)
    95121    (let ((info (procedure-information proc)))
     122      (unless (list? info)
     123        (error "Variable arguments are currently not supported"))
    96124      (and info (- (length info) 1))))
    97125 
     
    116144 
    117145  (with-output-to-string (lambda ()
    118                            (display "Ext.app.REMOTING_API = ")
     146                           (display (api-variable))
     147                           (display " = ")
    119148                           (json-write (wrap-modules  (alist->avector (map module-decl->direct-list (api)))))
    120149                           (display ";"))))
    121150
    122   
     151 
    123152
     153;; At this point we're done with the core of ext-direct. The following
     154;; procedures are integrations into existing server-side stacks like awful.
    124155
    125 
    126 ;; ;;converts the api-specification
    127 ;; ;;into the js-code needed to export the api to ext-js applications
    128 ;; (define (api-spec->ext-direct-api-declaration) #f)
    129 
    130 
    131 
    132 ;;TODO
    133 ;; factor readers and responders out
    134 ;; a request-reader is a procedure that
    135 ;; knows how to read the request and convert it into an ext-direct-request
    136 (define (awful-request-reader)
    137   (let* ((req (current-request))
    138          (is-form-request (form-request? req)))
    139     (values is-form-request
    140             (if is-form-request
    141                 (read-form-request  req)
    142                 (read-json-request req)))))
    143 
    144 (define spiffy-request-reader awful-request-reader)
    145 
    146 ;; responders
    147 ;; Responders are simply procedures that know about the details for
    148 ;; rendering answers to the browser.
    149 ;; A default awful responder looks like this :)
    150 (define (awful-responder data form-request?)
    151   (let ((content-type (if form-request? 'text/html 'application/json)))
    152     (parameterize ((awful-response-headers `((content-type ,content-type))))
    153       data)))
    154 
    155 ;; A spiffy responder might look like this :)
    156 (define (spiffy-responder data form-request?)
    157   (let ((content-type (if form-request? 'text/html 'application/json)))
    158     (with-headers `((content-type ,content-type)
    159                     (content-length ,(string-length data)))
    160                   (display data (response-port (current-response))))))
    161 
    162 
    163 
    164 (define (std-handler arg) (display arg))
    165 (api `((mod1 ((action1 ,std-handler) (action2 ,std-handler #t))) (mod2 ((action2 ,std-handler)))))
  • release/4/ext-direct/trunk/tests/run.scm

    r19451 r21032  
    33;;
    44;; Created: So Aug 15 12:34:54 2010 (CEST)
    5 ;; Last-Updated: So Aug 15 13:30:10 2010 (CEST)
     5;; Last-Updated: Sa Sep  4 08:33:46 2010 (CEST)
    66;;           By:
    77
     
    1010
    1111(test-begin)
    12 (test "encode-json-response"
    13       "{\"type\": \"remote\", \"tid\": \"1\", \"action\": \"foo\", \"method\": \"bar\", \"result\": [1, 2, 3], \"success\": true}"
    14       (let ((resp (make-response type: "remote"
    15                                  tid: "1"
    16                                  action: "foo"
    17                                  method: "bar"
    18                                  result: (list 1 2 3)
    19                                  success: #t)))
    20         (response->json resp)))
    2112
    22 (test "encode-multiple-responses"
    23       "[{\"type\": \"remote\", \"tid\": \"1\", \"action\": \"foo\", \"method\": \"bar\", \"result\": [1, 2, 3], \"success\": true},{\"type\": \"remote\", \"tid\": \"1\", \"action\": \"foo\", \"method\": \"bar\", \"result\": [1, 2, 3], \"success\": true}]"
    24       (let ((resp (make-response type: "remote"
    25                                  tid: "1"
    26                                  action: "foo"
    27                                  method: "bar"
    28                                  result: (list 1 2 3)
    29                                  success: #t)))
    30         (encode-json-response (list resp resp))))
     13(test-group "Encoding"
    3114
    32 (test "encode-form-response"
    33             "<html><body><textarea>{\"type\": \"remote\", \"tid\": \"1\", \"action\": \"foo\", \"method\": \"bar\", \"result\": [1, 2, 3], \"success\": true}</textarea></body></html>"
    34             (let ((resp (make-response type: "remote"
    35                                        tid: "1"
    36                                        action: "foo"
    37                                        method: "bar"
    38                                        result: (list 1 2 3)
    39                                        success: #t)))
    40               (encode-form-response resp)))
     15            (test "encode-json-response"
     16                  "{\"type\": \"remote\", \"tid\": \"1\", \"action\": \"foo\", \"method\": \"bar\", \"result\": [1, 2, 3], \"success\": true}"
     17                  (let ((resp (make-ext-direct-response type: "remote"
     18                                                        tid: "1"
     19                                                        action: "foo"
     20                                                        method: "bar"
     21                                                        result: (list 1 2 3)
     22                                                        success: #t)))
     23                    (response->json resp)))
     24
     25            (test "encode-multiple-responses"
     26                  "[{\"type\": \"remote\", \"tid\": \"1\", \"action\": \"foo\", \"method\": \"bar\", \"result\": [1, 2, 3], \"success\": true},{\"type\": \"remote\", \"tid\": \"1\", \"action\": \"foo\", \"method\": \"bar\", \"result\": [1, 2, 3], \"success\": true}]"
     27                  (let ((resp (make-ext-direct-response type: "remote"
     28                                                        tid: "1"
     29                                                        action: "foo"
     30                                                        method: "bar"
     31                                                        result: (list 1 2 3)
     32                                                        success: #t)))
     33                    (encode-json-response (list resp resp))))
     34
     35            (test "encode-form-response"
     36                  "<html><body><textarea>{\"type\": \"remote\", \"tid\": \"1\", \"action\": \"foo\", \"method\": \"bar\", \"result\": [1, 2, 3], \"success\": true}</textarea></body></html>"
     37                  (let ((resp (make-ext-direct-response type: "remote"
     38                                                        tid: "1"
     39                                                        action: "foo"
     40                                                        method: "bar"
     41                                                        result: (list 1 2 3)
     42                                                        success: #t)))
     43                    (encode-form-response resp))))
     44
     45
     46(test-group "api publishing"
     47            (test "empty-api"
     48                  "Ext.app.REMOTING_API = {\"url\": \"test\", \"type\": \"remoting\", \"actions\": {}};"
     49                  (parameterize ((api '()))
     50                    (api->json "test")))
     51
     52            (test "no-form-action"
     53                  "Ext.app.REMOTING_API = {\"url\": \"test\", \"type\": \"remoting\", \"actions\": {\"mod1\": [{\"name\": \"action1\", \"len\": 1, \"formHandler\": false}]}};"
     54                  (parameterize ((api `((mod1 ((action1 ,(lambda (x) x)))))))
     55                    (api->json "test")))
     56
     57            (test "form-action"
     58                  "Ext.app.REMOTING_API = {\"url\": \"test\", \"type\": \"remoting\", \"actions\": {\"mod1\": [{\"name\": \"action1\", \"len\": 1, \"formHandler\": true}]}};"
     59                  (parameterize ((api `((mod1 ((action1 ,(lambda (x) x) #t))))))
     60                    (api->json "test")))
     61
     62            (test "multiple actions"
     63          "Ext.app.REMOTING_API = {\"url\": \"test\", \"type\": \"remoting\", \"actions\": {\"mod1\": [{\"name\": \"a1\", \"len\": 1, \"formHandler\": false}, {\"name\": \"a2\", \"len\": 1, \"formHandler\": false}]}};"
     64                  (parameterize ((api `((mod1 ((a1 ,(lambda (x) x)) (a2 ,(lambda (x) x)))))))
     65                    (api->json "test")))
     66
     67            (test "multiple modules"
     68              "Ext.app.REMOTING_API = {\"url\": \"test\", \"type\": \"remoting\", \"actions\": {\"mod1\": [{\"name\": \"a1\", \"len\": 1, \"formHandler\": false}], \"mod2\": [{\"name\": \"a2\", \"len\": 1, \"formHandler\": false}]}};"
     69                  (parameterize ((api `((mod1 ((a1 ,(lambda (x) x)))) (mod2 ((a2 ,(lambda (x) x)))))))
     70                    (api->json "test")))
     71
     72            (test "procedure-arity 1"
     73                  "Ext.app.REMOTING_API = {\"url\": \"test\", \"type\": \"remoting\", \"actions\": {\"mod1\": [{\"name\": \"a1\", \"len\": 2, \"formHandler\": false}]}};"
     74                  (parameterize ((api `((mod1 ((a1 ,(lambda (x y) x)))))))
     75                    (api->json "test")))
     76
     77            (test "procedure-arity 0"
     78                  "Ext.app.REMOTING_API = {\"url\": \"test\", \"type\": \"remoting\", \"actions\": {\"mod1\": [{\"name\": \"a1\", \"len\": 0, \"formHandler\": false}]}};"
     79                  (parameterize ((api `((mod1 ((a1 ,(lambda () "foo")))))))
     80                    (api->json "test")))
     81
     82            (test-error "procedure-arity variable (error)"
     83                  (parameterize ((api `((mod1 ((a1 ,(lambda x "foo")))))))
     84                    (api->json "test"))))
    4185(test-end)
     86(unless (zero? (test-failure-count)) (exit 1))
Note: See TracChangeset for help on using the changeset viewer.