aboutsummaryrefslogtreecommitdiff
path: root/bin/mock_server.rb
blob: b33eac615479865f9b999957329cc4870d9f1a04 (plain)
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
#!/usr/bin/env ruby
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

# frozen_string_literal: true

require 'socket'
require 'json'

TCP_PORT = 8082

server = TCPServer.new TCP_PORT

puts "Provide some JSON and press Ctrl-D oder pipe the input via STDIN."
response = ''

while input = gets do
  response += input
end

puts "Starting the server on #{TCP_PORT}."

loop do
  client = server.accept

  puts 'Connection accepted'
  puts client.gets

  begin
    client.print("HTTP/1.1 200 OK\r\n")
    client.print("Content-Type: application/json\r\n")
    client.print("Content-Length: #{response.bytesize}\r\n")
    client.print("Connection: close\r\n")
    client.print("\r\n")

    response.each_char do |chunk|
      client.print chunk
      sleep 0.1
    end

    # client.print(response)
  end

  client.close
end