File: cassettes.rst

package info (click to toggle)
betamax 0.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 668 kB
  • sloc: python: 2,661; makefile: 125; sh: 13
file content (151 lines) | stat: -rw-r--r-- 3,567 bytes parent folder | download | duplicates (5)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
What is a cassette?
===================

A cassette is a set of recorded interactions serialized to a specific format.
Currently the only supported format is JSON_. A cassette has a list (or array)
of interactions and information about the library that recorded it. This means
that the cassette's structure (using JSON) is

.. code:: javascript

    {
      "http_interactions": [
        // ...
      ],
      "recorded_with": "betamax"
    }

Each interaction is the object representing the request and response as well
as the date it was recorded. The structure of an interaction is

.. code:: javascript

    {
      "request": {
        // ...
      },
      "response": {
        // ...
      },
      "recorded_at": "2013-09-28T01:25:38"
    }

Each request has the body, method, uri, and an object representing the
headers. A serialized request looks like:

.. code:: javascript

    {
      "body": {
        "string": "...",
        "encoding": "utf-8"
      },
      "method": "GET",
      "uri": "http://example.com",
      "headers": {
        // ...
      }
    }

A serialized response has the status_code, url, and objects
representing the headers and the body. A serialized response looks like:

.. code:: javascript

    {
      "body": {
        "encoding": "utf-8",
        "string": "..."
      },
      "url": "http://example.com",
      "status": {
        "code": 200,
        "message": "OK"
      },
      "headers": {
        // ...
      }
    }

If you put everything together, you get:

.. _cassette-dict:

.. code:: javascript

    {
      "http_interactions": [
        {
          "request": {
            {
              "body": {
                "string": "...",
                "encoding": "utf-8"
              },
              "method": "GET",
              "uri": "http://example.com",
              "headers": {
                // ...
              }
            }
          },
          "response": {
            {
              "body": {
                "encoding": "utf-8",
                "string": "..."
              },
              "url": "http://example.com",
              "status": {
                "code": 200,
                "message": "OK"
              },
              "headers": {
                // ...
              }
            }
          },
          "recorded_at": "2013-09-28T01:25:38"
        }
      ],
      "recorded_with": "betamax"
    }

If you were to pretty-print a cassette, this is vaguely what you would see.
Keep in mind that since Python does not keep dictionaries ordered, the items
may not be in the same order as this example.

.. note::

    **Pro-tip** You can pretty print a cassette like so:
    ``python -m json.tool cassette.json``.

What is a cassette library?
===========================

When configuring Betamax, you can choose your own cassette library directory.
This is the directory available from the current directory in which you want
to store your cassettes.

For example, let's say that you set your cassette library to be
``tests/cassettes/``. In that case, when you record a cassette, it will be
saved there. To continue the example, let's say you use the following code:

.. code:: python

    from requests import Session
    from betamax import Betamax


    s = Session()
    with Betamax(s, cassette_library_dir='tests/cassettes').use_cassette('example'):
        r = s.get('https://httpbin.org/get')

You would then have the following directory structure::

    .
    `-- tests
        `-- cassettes
            `-- example.json

.. _JSON: http://json.org