~fnux/matrix-yggdrasil-http-proxy

b210c2d1f319ed452ce92946871fb038afc45449 — Timothée Floure 4 years ago 1b4d846
Add simple HTTP GET integration test
3 files changed, 57 insertions(+), 10 deletions(-)

M http.go
M run-integration-tests.sh
A tests/pong-http-server.rb
M http.go => http.go +1 -1
@@ 58,7 58,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Determine yggdrasil target.
	targetHeader := "x-yggdrasil-target"
	var target string = r.Header.Get(targetHeader)
	if len(target) != crypto.NodeIDLen {
	if (len(target) / 2) != crypto.NodeIDLen {
		logger.Warnf("HTTP: could not determine target Yggdrasil NodeID from %s header.", targetHeader)
		if !(len(*coapTarget) > 0) {
			logger.Error("HTTP: no way to determine target Yggdrasil NodeID. Rejecting.")

M run-integration-tests.sh => run-integration-tests.sh +16 -9
@@ 8,11 8,11 @@ TMPDIR=$(mktemp -d)

NODE_A_CONF="$TMPDIR/node-A.conf"
NODE_A_PROXY_PORT=8080
NODE_A_TARGET="http://127.0.0.1:9090"
NODE_A_TARGET_PORT=9090

NODE_B_CONF="$TMPDIR/node-B.conf"
NODE_B_PROXY_PORT=8081
NODE_B_TARGET="http://127.0.0.1:9091"
NODE_B_TARGET_PORT=9091

if [ ! -x "$BIN" ]; then
	echo "Cannot execute $BIN." >&2


@@ 20,7 20,7 @@ fi

# Generate Yggdrasil configurations.
./$BIN -gen-conf > "$NODE_A_CONF"
sed -i -e "s/  Peers: \[\]/  Peers: \[ \'tcp:\/\/127.0.0.1:54321\' \]/" "$NODE_A_CONF"
sed -i -e "s/  Listen: \[\]/  Listen: \[ \'tcp:\/\/127.0.0.1:54321\' \]/" "$NODE_A_CONF"
./$BIN -gen-conf > "$NODE_B_CONF"
sed -i -e "s/  Peers: \[\]/  Peers: \[ \'tcp:\/\/127.0.0.1:54321\' \]/" "$NODE_B_CONF"



@@ 34,15 34,22 @@ node_b_id=$(./$BIN -use-conf-file "$NODE_B_CONF" -get-node-id | tail -n 1)
# Launch both nodes.
./$BIN -use-conf-file "$NODE_A_CONF" \
	-http-port $NODE_A_PROXY_PORT &
	-http-target $NODE_A_TARGET &
	-http-target "http://127.0.0.1:$NODE_A_TARGET_PORT/" &
sleep 1
./$BIN -use-conf-file "$NODE_B_CONF" \
	-http-port $NODE_B_PROXY_PORT \
	-http-target $NODE_B_TARGET &
	-http-target "http://127.0.0.1:$NODE_B_TARGET_PORT/" &

# Run dumb HTTP servers.
./tests/pong-http-server.rb $NODE_A_TARGET_PORT &
./tests/pong-http-server.rb $NODE_B_TARGET_PORT &

# Wait for nodes to be properly connected.
sleep 1

# TODO: Run integration tests :-)
nc -l 9091 &
curl "http://127.0.0.1:$NODE_A_PROXY_PORT/" -H "x-yggdrasil-target: $node_b_id" &
sleep 1
##
# Run integration tests.

echo "--- Simple HTTP GET ---"
curl "http://127.0.0.1:$NODE_A_PROXY_PORT/" -H "x-yggdrasil-target: $node_b_id"
echo "-----------------------"

A tests/pong-http-server.rb => tests/pong-http-server.rb +40 -0
@@ 0,0 1,40 @@
#!/usr/bin/ruby

require 'socket'

port = ARGV[0]
server = TCPServer.new('127.0.0.1', port)

puts "Listening for HTTP requests on port #{port}."

loop do
  # Wait until a client connects, then return a TCPSocket
  # that can be used in a similar fashion to other Ruby
  # I/O objects. (In fact, TCPSocket is a subclass of IO.)
  socket = server.accept

  # Read the first line of the request (the Request-Line)
  request = socket.gets

  # Log the request to the console for debugging
  puts request

  # Define response
  response = "{'value': 'pong'}"

  # Print headers
  socket.print "HTTP/1.1 200 OK\r\n" +
               "Content-Type: application/json\r\n" +
               "Content-Length: #{response.bytesize}\r\n" +
               "Connection: close\r\n"

  # Print a blank line to separate the header from the response body,
  # as required by the protocol.
  socket.print "\r\n"

  # Print the actual response body, which is just "Hello World!\n"
  socket.print response

  # Close the socket, terminating the connection
  socket.close
end