Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aee0282493d935f60376f9d11797d726f578ef14 (plain) (blame)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.internal.http.tests;

import org.eclipse.net4j.channel.IChannel;
import org.eclipse.net4j.http.HTTPUtil;
import org.eclipse.net4j.internal.http.HTTPClientConnector;
import org.eclipse.net4j.tests.AbstractTransportTest;
import org.eclipse.net4j.tests.signal.IntRequest;
import org.eclipse.net4j.tests.signal.TestSignalClientProtocolFactory;
import org.eclipse.net4j.tests.signal.TestSignalProtocol;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.io.ExtendedDataInputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author Eike Stepper
 */
public class HTTPTest extends AbstractTransportTest
{
  public HTTPTest()
  {
  }

  @Override
  protected IManagedContainer createContainer()
  {
    IManagedContainer container = super.createContainer();
    HTTPUtil.prepareContainer(container);
    container.registerFactory(new TestSignalClientProtocolFactory());
    return container;
  }

  /**
   * Result: With the current implementation (HttpClient / Jetty) it's not possible to transfer request data before
   */
  public void _testRequestFlush() throws Exception
  {
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod("http://eike@localhost:8080/net4j/echotest");
    method.setRequestEntity(new RequestEntity()
    {
      public long getContentLength()
      {
        return -1;
      }

      public String getContentType()
      {
        return "application/octet-stream";
      }

      public boolean isRepeatable()
      {
        return false;
      }

      public void writeRequest(OutputStream out) throws IOException
      {
        int count = 10;
        out.write(count);
        for (int i = 0; i < count; i++)
        {
          send(out, i);
        }
      }

      private void send(OutputStream out, int b) throws IOException
      {
        try
        {
          System.out.println("Writing " + b);
          out.write(b);
          out.flush();
          Thread.sleep(1000);
        }
        catch (InterruptedException ex)
        {
          throw new RuntimeException(ex);
        }
      }
    });

    client.executeMethod(method);
    InputStream responseBody = method.getResponseBodyAsStream();
    ExtendedDataInputStream in = new ExtendedDataInputStream(responseBody);
    int count = in.readInt();
    for (int i = 0; i < count; i++)
    {
      int b = in.readByte();
      assertEquals(i, b);

      long gap = in.readLong();
      System.out.println("Gap: " + gap);
    }

    method.releaseConnection();
  }

  public void test1() throws Exception
  {
    HTTPClientConnector connector = getHTTPConnector();
    IChannel channel = connector.openChannel(TestSignalProtocol.PROTOCOL_NAME, null);

    IntRequest request = new IntRequest(channel, 305419896);
    int result = request.send();
    assertEquals(305419896, result);
    channel.close();
    connector.deactivate();
  }

  private HTTPClientConnector getHTTPConnector()
  {
    return (HTTPClientConnector)container.getElement("org.eclipse.net4j.connectors", "http",
        "http://eike@localhost:8080/net4j");
  }
}

Back to the top