@workInProgress @ngdoc overview @name Cookbook: Hello World @description
Your name:
Hello {{name}}!
it('should change the binding when user enters text', function() { expect(binding('name')).toEqual('World'); input('name').enter('angular'); expect(binding('name')).toEqual('angular'); });
# Things to notice Take a look through the source and note: * The script tag that {@link guide/dev_guide.bootstrap bootstraps} the angular environment. * The text {@link api/angular.widget.input input widget} which is bound to the greeting name text. * No need for listener registration and event firing on change events. * The implicit presence of the `name` variable which is in the root {@link api/angular.scope scope}. * The double curly brace `{{markup}}`, which binds the name variable to the greeting text. * The concept of {@link guide/dev_guide.templates.databinding data binding}, which reflects any changes to the input field in the greeting text. er-api'>model-serializer-api Web APIs for Django.
aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/tests/htmlrenderer.py
blob: 4caed59ee0420ab04653297ab28c189ef940c327 (plain)
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
from django.core.exceptions import PermissionDenied
from django.conf.urls.defaults import patterns, url
from django.http import Http404
from django.test import TestCase
from django.template import TemplateDoesNotExist, Template
import django.template.loader
from rest_framework.decorators import api_view, renderer_classes
from rest_framework.renderers import TemplateHTMLRenderer
from rest_framework.response import Response


@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer,))
def example(request):
    """
    A view that can returns an HTML representation.
    """
    data = {'object': 'foobar'}
    return Response(data, template_name='example.html')


@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer,))
def permission_denied(request):
    raise PermissionDenied()


@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer,))
def not_found(request):
    raise Http404()


urlpatterns = patterns('',
    url(r'^$', example),
    url(r'^permission_denied$', permission_denied),
    url(r'^not_found$', not_found),
)


class TemplateHTMLRendererTests(TestCase):
    urls = 'rest_framework.tests.htmlrenderer'

    def setUp(self):
        """
        Monkeypatch get_template
        """
        self.get_template = django.template.loader.get_template

        def get_template(template_name):
            if template_name == 'example.html':
                return Template("example: {{ object }}")
            raise TemplateDoesNotExist(template_name)

        django.template.loader.get_template = get_template

    def tearDown(self):
        """
        Revert monkeypatching
        """
        django.template.loader.get_template = self.get_template

    def test_simple_html_view(self):
        response = self.client.get('/')
        self.assertContains(response, "example: foobar")
        self.assertEquals(response['Content-Type'], 'text/html')

    def test_not_found_html_view(self):
        response = self.client.get('/not_found')
        self.assertEquals(response.status_code, 404)
        self.assertEquals(response.content, "404 Not Found")
        self.assertEquals(response['Content-Type'], 'text/html')

    def test_permission_denied_html_view(self):
        response = self.client.get('/permission_denied')
        self.assertEquals(response.status_code, 403)
        self.assertEquals(response.content, "403 Forbidden")
        self.assertEquals(response['Content-Type'], 'text/html')


class TemplateHTMLRendererExceptionTests(TestCase):
    urls = 'rest_framework.tests.htmlrenderer'

    def setUp(self):
        """
        Monkeypatch get_template
        """
        self.get_template = django.template.loader.get_template

        def get_template(template_name):
            if template_name == '404.html':
                return Template("404: {{ detail }}")
            if template_name == '403.html':
                return Template("403: {{ detail }}")
            raise TemplateDoesNotExist(template_name)

        django.template.loader.get_template = get_template

    def tearDown(self):
        """
        Revert monkeypatching
        """
        django.template.loader.get_template = self.get_template

    def test_not_found_html_view_with_template(self):
        response = self.client.get('/not_found')
        self.assertEquals(response.status_code, 404)
        self.assertEquals(response.content, "404: Not found")
        self.assertEquals(response['Content-Type'], 'text/html')

    def test_permission_denied_html_view_with_template(self):
        response = self.client.get('/permission_denied')
        self.assertEquals(response.status_code, 403)
        self.assertEquals(response.content, "403: Permission denied")
        self.assertEquals(response['Content-Type'], 'text/html')