Benoît Hubert il y a 7 ans
Parent
commit
1c1072f1c4

+ 0 - 12
exemples/5-mini-serveur-http/mini-serveur-ft.py

@@ -1,12 +0,0 @@
-from selenium import webdriver
-
-# Instance de Firefox contrôlée par webdriver
-browser = webdriver.Firefox()
-
-# On pointe ce browser vers l'URL de notre mini-serveur
-browser.get('http://localhost:8081')
-
-# En toute logique, on voudrait que le HTML généré par notre serveur ait un titre
-assert 'Mini-Serveur' in browser.title
-
-browser.quit()

+ 11 - 1
exemples/5-mini-serveur-http/mini-serveur.py

@@ -5,15 +5,25 @@ base_html = """<!DOCTYPE html>
   <head>
     <title>Mini-Serveur</title>
   </head>
-  <body>{body}</body>
+  <body>
+    {body}
+  </body>
 </html>
 """
 
 # HTTPRequestHandler class
 class MiniHTTPServerRequestHandler(BaseHTTPRequestHandler):
 
+  paths_GET = ["/", "/login", "/register"]
+
   # GET
   def do_GET(self):
+        print(self.path)
+        if not self.path in self.paths_GET:
+            self.send_response(404)
+            self.end_headers()
+            return
+
         # Send response status code
         self.send_response(200)
 

+ 62 - 0
exemples/5-mini-serveur-http/tests-fonctionnels.py

@@ -0,0 +1,62 @@
+from selenium import webdriver
+import unittest
+import requests
+
+class NewVisitorTest(unittest.TestCase):
+
+    def setUp(self):
+        self.browser = webdriver.Firefox()
+
+    def tearDown(self):
+        self.browser.quit()
+
+    def test_mini_server_home(self):
+        # Edith has heard about a cool new online to-do app. She goes
+        # to check out its homepage
+        self.browser.get('http://localhost:8081')
+
+        # She notices the page title and header mention to-do lists
+        self.assertIn('Mini-Serveur', self.browser.title)
+
+        r = requests.get("http://localhost:8081")
+        status_code = r.status_code
+        self.assertEqual(status_code, 200)
+
+        #print(self.browser.find_elements_by_tag_name('a'))
+
+
+        # She is invited to enter a to-do item straight away
+
+#        print(browser.find_elements_by_tag_name('a'))
+
+    def test_mini_server_not_found(self):
+        # Edith has heard about a cool new online to-do app. She goes
+        # to check out its homepage
+        self.browser.get('http://localhost:8081/not-found')
+
+        # She notices the page title and header mention to-do lists
+        r = requests.get("http://localhost:8081/not-found")
+        status_code = r.status_code
+        self.assertEqual(status_code, 404)
+
+
+
+# # Instance de Firefox contrôlée par webdriver
+# browser = webdriver.Firefox()
+
+# # On pointe ce browser vers l'URL de notre mini-serveur
+# browser.get('http://localhost:8081')
+
+# # En toute logique, on voudrait que le HTML généré par notre serveur ait un titre
+# assert 'Mini-Serveur' in browser.title
+
+# r = requests.get("http://localhost:8081")
+# # print(r.status_code)
+
+# print(browser.find_elements_by_tag_name('a'))
+
+# browser.quit()
+
+
+if __name__ == '__main__':
+    unittest.main(warnings='ignore')

+ 31 - 1
react-tuto/src/markdown/5. Mini-serveur web/05. Et les tests dans tout ça.md

@@ -30,10 +30,11 @@ C'est là que va intervenir Selenium : il permet de contrôler un navigateur (ic
 
 Avant d'aller plus loin et d'ajouter d'autres choses à notre mini-serveur, on va écrire un test dans lequel on va décrire ce qu'on voudrait que notre page web affiche, et comment on voudrait pouvoir interagir avec.
 
-Commençons par faire simple :
+Commençons par faire simple, avec ce premier test fonctionnel :
 
 ```python
 from selenium import webdriver
+import requests
 
 # Instance de Firefox contrôlée par webdriver
 browser = webdriver.Firefox()
@@ -44,5 +45,34 @@ browser.get('http://localhost:8081')
 # En toute logique, on voudrait que le HTML généré par notre serveur ait un titre
 assert 'Mini-Serveur' in browser.title
 
+r = requests.get("http://localhost:8081")
+
 browser.quit()
+```
+
+Si on lance ce test, il échoue, car en l'état actuel, notre mini-serveur ne renvoie pas vraiment de HTML, mais un simple "Hello world!".
+
+Pour faire passer ce test, retournons dans le fichier du serveur et ajoutons avant la classe `MiniHTTPServerRequestHandler` :
+
+```python
+
+base_html = """<!DOCTYPE html>
+<html>
+  <head>
+    <title>Mini-Serveur</title>
+  </head>
+  <body>{body}</body>
+</html>
+"""
+```
+
+Puis remplaçons :
+
+```python
+        message = "Hello world!"
+```
+Par ceci :
+
+```python
+        message = base_html.replace("{body}", "Hello world!")
 ```

Fichier diff supprimé car celui-ci est trop grand
+ 1 - 1
react-tuto/src/resources/markdown.json