#!/usr/bin/env python
"""End to end tests for the example programs.

This test assumes the examples have already been built.

At the moment top_buiddir is not in the environment, but top_builddir would be
more reliable than '..', so it's worth trying to pull it from the environment.
"""

__author__ = 'Jim Morrison <jim@twist.com>'


import os
import subprocess
import sys
import time
import unittest


_PORT = 9893


def _run_server(port, args):
  srcdir = os.environ.get('srcdir', '.')
  testdata = '%s/testdata' % srcdir
  top_builddir = os.environ.get('top_builddir', '..')
  base_args = ['%s/examples/spdyd' % top_builddir, str(port), '-d', testdata,
               '%s/privkey.pem' % testdata, '%s/cacert.pem' % testdata]
  if args:
    base_args.extend(args)
  return subprocess.Popen(base_args)

def _check_server_up(port):
  # Check this check for now.
  time.sleep(1)

def _kill_server(server):
  while server.returncode is None:
    server.terminate()
    time.sleep(1)
    server.poll()


class EndToEndSpdyTests(unittest.TestCase):
  @classmethod
  def setUpClass(cls):
    cls.server = _run_server(_PORT, None)
    _check_server_up(_PORT)

  @classmethod
  def tearDownClass(cls):
    _kill_server(cls.server)

  def setUp(self):
    build_dir = os.environ.get('top_builddir', '..')
    self.client = '%s/examples/spdycat' % build_dir

  def testSimpleRequest(self):
    self.assertEquals(
        0, subprocess.call([self.client, 'http://localhost:%d/' % _PORT]))


class EndToEndSpdy3Tests(unittest.TestCase):
  @classmethod
  def setUpClass(cls):
    cls.server = _run_server(_PORT, '-3')
    _check_server_up(_PORT)

  @classmethod
  def tearDownClass(cls):
    _kill_server(cls.server)

  def setUp(self):
    build_dir = os.environ.get('top_builddir', '..')
    self.client = '%s/examples/spdycat' % build_dir

  def testSimpleRequest(self):
    self.assertEquals(
        0, subprocess.call([self.client, 'http://localhost:%d/' % _PORT]))


if __name__ == '__main__':
  unittest.main()

@@ -360,19 +360,8 @@
 ess%0A
-import sys%0A
 impo
@@ -615,19 +615,8 @@
 dir,
- str(port),
  '-d
@@ -630,24 +630,78 @@
 data
-,%0A              
+%5D%0A  if args:%0A    base_args.extend(args)%0A  base_args.extend(%5Bstr(port),
  '%25s
@@ -725,16 +725,36 @@
 estdata,
+%0A                   
  '%25s/cac
@@ -777,45 +777,8 @@
 ata%5D
-%0A  if args:%0A    base_args.extend(args
 )%0A  
@@ -1084,32 +1084,102 @@
 etUpClass(cls):%0A
+    cls.setUpServer(%5B%5D)%0A%0A  @classmethod%0A  def setUpServer(cls, args):%0A
     cls.server =
@@ -1198,20 +1198,20 @@
 (_PORT, 
-None
+args
 )%0A    _c
@@ -1432,90 +1432,86 @@
 dir%0A
-%0A  def testSimpleRequest(self):%0A    self.assertEquals(%0A        0, subprocess.call(
+    self.stdout = 'No output'%0A%0A  def call(self, path, args):%0A     full_args = 
 %5Bsel
@@ -1511,33 +1511,32 @@
  = %5Bself.client,
- 
 'http://localhos
@@ -1543,501 +1543,762 @@
 t:%25d
-/
+%25s
 ' %25 
+(
 _PORT
-%5D))%0A%0A%0Aclass EndToEndSpdy3Tests(unittest.TestCase):%0A  @classmethod%0A  def setUpClass(cls):%0A    cls.server = _run_server(_PORT, '-3')%0A    _check_server_up(_PORT)%0A%0A  @classmethod%0A  def tearDownClass(cls):%0A    _kill_server(cls.server)%0A%0A  def setUp(self):%0A    build_dir = os.environ.get('top_builddir', '..')%0A    self.client = '%25s/examples/spdycat' %25 build_dir%0A%0A  def testSimpleRequest(self):%0A    self.assertEquals(%0A        0, subprocess.call(%5Bself.client, 'http://localhost:%25d/' %25 _PORT%5D)
+, path)%5D + args%0A     p = subprocess.Popen(full_args, stdout=subprocess.PIPE,%0A                          stdin=subprocess.PIPE)%0A     self.stdout, self.stderr = p.communicate()%0A     return p.returncode%0A%0A%0Aclass EndToEndSpdy2Tests(EndToEndSpdyTests):%0A  def testSimpleRequest(self):%0A    self.assertEquals(0, self.call('/', %5B%5D))%0A%0A  def testSimpleRequestSpdy3(self):%0A    self.assertEquals(0, self.call('/', %5B'-v', '-3'%5D))%0A    self.assertIn('NPN selected the protocol: spdy/3', self.stdout)%0A%0A%0Aclass EndToEndSpdy3Tests(EndToEndSpdyTests):%0A  @classmethod%0A  def setUpClass(cls):%0A    cls.setUpServer(%5B'-3'%5D)%0A%0A  def testSimpleRequest(self):%0A    self.assertEquals(0, self.call('/', %5B'-v'%5D))%0A    self.assertIn('NPN selected the protocol: spdy/3', self.stdout
 )%0A%0A%0A

Verify that the expected version of spdy is used.