« index

Coverage for /Users/kris/q-io/http-apps/content.js : 82%

91 lines | 75 run | 16 missing | 0 partial | 12 blocks | 2 blocks run | 10 blocks missing

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

  var Negotiate = require("./negotiate");
  var QS = require("qs");
  var URL = require("url2");
  
  /**
   * Makes an app that returns a response with static content
   * from memory.
   * @param {Body} body a Q-JSGI
   * response body
   * @param {String} contentType
   * @param {Number} status
   * @returns {App} a Q-JSGI app
   */
  exports.Content = function (body, contentType, status) {
      return function (request, response) {
          return {
              "status": status || 200,
              "headers": {
                  "content-type": contentType || "text/plain"
              },
              "body": body || ""
          };
      };
  };
  
  /**
   * Returns a Q-JSGI response with the given content.
   * @param {Body} content (optional) defaults to `[""]`
   * @param {String} contentType (optional) defaults to `"text/plain"`
   * @param {Number} status (optional) defaults to `200`
   * @returns {Response}
   */
  exports.content =
  exports.ok = function (content, contentType, status) {
      status = status || 200;
      content = content || "";
      if (typeof content === "string") {
          content = [content];
      }
      contentType = contentType || "text/plain";
      return {
          "status": status,
          "headers": {
              "content-type": contentType
          },
          "body": content
      };
  };
  
  /**
   * Wraps an app such that it expects to receive content
   * in the request body and passes that content as a string
   * to as the second argument to the wrapped JSGI app.
   *
   * @param {Function(Request, Object):Response} app
   * @returns {App}
   */
  exports.ContentRequest = function (app) {
      return function (request, response) {
          return Q.when(request.body.read(), function (body) {
              return app(body, request, response);
          });
      };
  };
  
  /**
   * @param {Function(Request):Object}
   * @returns {App}
   */
  exports.Inspect = function (app) {
      return Negotiate.Method({"GET": function (request, response) {
          return Q.when(app(request, response), function (object) {
              return {
                  status: 200,
                  headers: {
                      "content-type": "text/plain"
                  },
                  body: [inspect(object)]
              }
          });
      }});
  };
  
  /**
   */
  exports.ParseQuery = function (app) {
      return function (request, response) {
          request.query = QS.parse(URL.parse(request.url).query || "");
          return app(request, response);
      };
  };
« index | cover.io