Commits

Ian Lewis committed 1fff7c4 Merge

merge

  • Participants
  • Parent commits ab8e7cf, b592dd1

Comments (0)

Files changed (3)

File bpmobile/middleware.py

     cache_key_name = 'session_key_%s'
 
     def get_agent(self, request):
-        return uamobile.detect(request.META)
+        return getattr(request, 'agent', uamobile.detect(request.META))
 
     def get_cache_key(self, guid):
         return self.cache_key_name % guid
             if request.method == 'GET' and not request.GET.has_key('guid'):
                 # guid=onを付与したURLにリダイレクト
                 if request.is_secure():
+                    # SSLだとiモードIDは使えない
                     protocol = 'https'
                 else:
                     protocol = 'http'
                 if request.GET:
-                    query_string = '&'.join([request.GET.urlencode(), 'guid=on'])
+                    query_string = '&guid=on'
                 else:
-                    query_string = 'guid=on'
-                url = "%s://%s%s?%s" % (protocol, request.get_host(), request.get_full_path(), query_string)
+                    query_string = '?guid=on'
+                url = "%s://%s%s%s" % (protocol, request.get_host(), request.get_full_path(), query_string)
                 return HttpResponseRedirect(url)
             if agent.guid:
                 # cacheからセッションキーをとってくる

File bpmobile/test.py

+# coding:utf-8
+from django.test import TestCase as DjangoTestCase
+
+import uamobile
+
+class TestCase(DjangoTestCase):
+    def _pre_setup(self):
+        super(TestCase, self)._pre_setup()
+
+        self.agent_docomo = uamobile.docomo.DoCoMoUserAgent({}, {})
+        self.agent_ezweb = uamobile.ezweb.EZwebUserAgent({}, {})
+        self.agent_softbank = uamobile.softbank.SoftBankUserAgent({}, {})

File bpmobile/tests.py

-from django.test import TestCase
 from django.http import HttpRequest
+from django.conf import settings
 from django.template import Template, RequestContext
-import uamobile
 
+from test import TestCase
+from middleware import BPMobileSessionMiddleware
 from templatetags import mobile
+from wsgi import DetectEncodingWSGIRequest
 
 class TemplateTagTest(TestCase):
-    def setUp(self):
-        self.agent_docomo = uamobile.docomo.DoCoMoUserAgent({}, {})
-        self.agent_ezweb = uamobile.ezweb.EZwebUserAgent({}, {})
-        self.agent_softbank = uamobile.softbank.SoftBankUserAgent({}, {})
-
     def test_emoji_tag_docomo(self):
         req = HttpRequest()
         req.agent = self.agent_docomo
         except:
             self.fail()
         self.failUnlessEqual(content, u'\ue63e')
+
+class SessionMiddlewareTest(TestCase):
+    def test_guid_redirect(self):
+        if not 'django.contrib.sessions.middleware.SessionMiddleware' in settings.MIDDLEWARE_CLASSES:
+            self.fail('Django session middleware is not installed.')
+
+        req = DetectEncodingWSGIRequest({
+            'SERVER_NAME': 'example.com',
+            'SERVER_PORT': 80,
+            'QUERY_STRING': 'abc=def&foo=bar',
+            'REQUEST_METHOD': 'GET',
+        })
+        req.agent = self.agent_docomo
+
+        middleware = BPMobileSessionMiddleware()
+
+        res = middleware.process_request(req)
+        self.failUnless(res, 'not redirected')
+        self.failUnlessEqual(res.status_code, 302)
+        self.failUnlessEqual(res['Location'], 'http://example.com/?abc=def&foo=bar&guid=on')