From 7c8af8954bd95123dbf324a360a4c50956ebeab5 Mon Sep 17 00:00:00 2001
From: Jean-Baptiste <bleme@pm.me>
Date: Mon, 3 Jun 2019 07:58:33 +0200
Subject: [PATCH] update: add location header on every 200 responses

---
 djangoldp/tests/tests_update.py | 20 ++++++++++++++++++++
 djangoldp/views.py              |  4 +++-
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/djangoldp/tests/tests_update.py b/djangoldp/tests/tests_update.py
index cbd74bc9..c94facfe 100644
--- a/djangoldp/tests/tests_update.py
+++ b/djangoldp/tests/tests_update.py
@@ -1,12 +1,22 @@
 from django.contrib.auth.models import User
 from django.test import TestCase
+from rest_framework.test import APIRequestFactory, APIClient
+from rest_framework.utils import json
 
 from djangoldp.serializers import LDPSerializer
+from djangoldp.tests.models import Post
 from djangoldp.tests.models import Skill, JobOffer, Conversation, Message
 
 
 class Update(TestCase):
 
+    def setUp(self):
+        self.factory = APIRequestFactory()
+        self.client = APIClient()
+
+    def tearDown(self):
+        pass
+
     def test_update(self):
         skill = Skill.objects.create(title="to drop", obligatoire="obligatoire", slug="slug1")
         skill1 = Skill.objects.create(title="skill1", obligatoire="obligatoire", slug="slug2")
@@ -250,3 +260,13 @@ class Update(TestCase):
         self.assertEquals(messages[0].text, "Message 1 UP")
         self.assertEquals(messages[1].text, "Message 2 UP")
         self.assertEquals(messages[2].text, "Message 3 NEW")
+
+    def test_put_resource(self):
+        post = Post.objects.create(content="content")
+        body = [{
+            '@id': '/posts/{}/'.format(post.pk),
+            'http://happy-dev.fr/owl/#content': "post content"}]
+        response = self.client.put('/posts/{}/'.format(post.pk), data=json.dumps(body), content_type='application/ld+json')
+        self.assertEqual(response.status_code, 200)
+        self.assertEquals(response.data['content'], "post content")
+        self.assertIn('location', response._headers)
diff --git a/djangoldp/views.py b/djangoldp/views.py
index f5056c87..51cfd453 100644
--- a/djangoldp/views.py
+++ b/djangoldp/views.py
@@ -140,8 +140,10 @@ class LDPViewSet(LDPViewSetGenerator):
         response["Access-Control-Expose-Headers"] = "Location"
         response["Access-Control-Allow-Credentials"] = 'true'
         response["Accept-Post"] = "application/ld+json"
-        if response.status_code == 201 and '@id' in response.data:
+        if response.status_code in [201, 200] and '@id' in response.data:
             response["Location"] = response.data['@id']
+        else:
+            pass
         response["Accept-Post"] = "application/ld+json"
         if request.user.is_authenticated():
             try:
-- 
GitLab