<commit_before># -*- coding: utf-8 -*-
require "spec_helper"

describe RabbitMQ::HTTP::Client do
  let(:endpoint) { "http://127.0.0.1:15672" }

  subject do
    described_class.connect(endpoint, :username => "guest", :password => "<PASSWORD>")
  end

  before :each do
    @conn = Bunny.new
    @conn.start
  end

  after :each do
    @conn.close
  end

  #
  # Helpers
  #

  # Statistics tables in the server are updated asynchronously,
  # in particular starting with rabbitmq/rabbitmq-management#236,
  # so in some cases we need to wait before GET'ing e.g. a newly opened connection.
  def await_event_propagation
    # same number as used in rabbit-hole test suite. Works OK.
    sleep 1
  end

  #
  # Default endpoint path
  #
  describe "default endpoint path" do
    it "does NOT append '/api' if the endpoint provides a path" do
      c = described_class.connect("http://guest:guest@127.0.0.1:15672/api")

      r = c.overview
      expect(r.rabbitmq_version).to_not be_nil
      expect(r.erlang_version).to_not be_nil
    end
  end


  #
  # URI-only access
  #

  describe "URI-only access" do
    it "authenticates successfully" do
      c = described_class.connect("http://guest:guest@127.0.0.1:15672")

      r = c.overview
      expect(r.rabbitmq_version).to_not be_nil
      expect(r.erlang_version).to_not be_nil
    end
  end


  #
  # Overview
  #

  describe "GET /api/overview" do
    it "returns an overview" do
      r = subject.overview

      ts = r.exchange_types.map { |h| h.name }.
        sort
      ["direct", "fanout", "headers", "topic"].each do |t|
        expect(ts).to include(t)
      end

      expect(r.rabbitmq_version).to_not be_nil
      expect(r.erlang_version).to_not be_nil
    end
  end

  describe "#enabled_protocols" do
    it "returns a list of enabled protocols" do
      xs = subject.enabled_protocols

      expect(xs).to include("amqp")
    end
  end

  describe "#enabled_protocols" do
    it "returns a list of enabled protocols" do
      xs = subject.protocol_ports

      # hash of protocol => port
      expect(xs["amqp"]).to eq(5672)
    end
  end

  #
  # Nodes
  #

  describe "GET /api/nodes" do
    it "lists cluster nodes with detailed status information for each one of them" do
      nodes  = subject.list_nodes
      n      = nodes.first

      rabbit = n.applications.detect { |app| app.name == "rabbit" }
      expect(rabbit.description).to eq("RabbitMQ")

      expect(n.name).to match(/^rabbit/)
    end
  end

  describe "GET /api/node/:name" do
    it "returns status information for a single cluster node" do
      ns = subject.list_nodes
      n  = subject.node_info(ns.first.name)

      rabbit = n.applications.detect { |app| app.name == "rabbit" }
      expect(rabbit.description).to eq("RabbitMQ")

      expect(n.name).to match(/^rabbit/)
    end
  end

  #
  # Extensions
  #

  describe "GET /api/extensions" do
    it "returns a list of enabled management plugin extensions" do
      xs = subject.list_extensions

      expect(xs).to be_kind_of(Array)
    end
  end

  #
  # Definitions
  #

  describe "GET /api/definitions" do
    it "returns a list of all resources/definitions (vhosts, users, permissions, queues, exchanges, bindings, etc)" do
      xs = subject.list_definitions

      expect(xs.bindings).to be_instance_of(Array)
      expect(xs.queues).to be_instance_of(Array)
      expect(xs.exchanges).to be_instance_of(Array)
      expect(xs.users).to be_instance_of(Array)
      expect(xs.vhosts).to be_instance_of(Array)
    end
  end

  describe "POST /api/definitions" do
    let(:queue_name) { 'my-definition-queue' }

    let(:definition) do
      {
        :queues => [{
          :name => queue_name,
          :vhost => '/',
          durable: true,
          :auto_delete =>  false,
          :arguments => {
             "x-dead-letter-exchange" => 'dead'
          }
        }]
      }.to_json
    end

    it "returns true when successful" do
      r = subject.upload_definitions(definition)
      expect(r).to eq(true)

      subject.delete_queue("/", queue_name)
    end
<commit_msg>Adapt this test for RabbitMQ 3.6.7 and latest Hashie<commit_after># -*- coding: utf-8 -*-
require "spec_helper"

describe RabbitMQ::HTTP::Client do
  let(:endpoint) { "http://127.0.0.1:15672" }

  subject do
    described_class.connect(endpoint, :username => "guest", :password => "<PASSWORD>")
  end

  before :each do
    @conn = Bunny.new
    @conn.start
  end

  after :each do
    @conn.close
  end

  #
  # Helpers
  #

  # Statistics tables in the server are updated asynchronously,
  # in particular starting with rabbitmq/rabbitmq-management#236,
  # so in some cases we need to wait before GET'ing e.g. a newly opened connection.
  def await_event_propagation
    # same number as used in rabbit-hole test suite. Works OK.
    sleep 1
  end

  #
  # Default endpoint path
  #
  describe "default endpoint path" do
    it "does NOT append '/api' if the endpoint provides a path" do
      c = described_class.connect("http://guest:guest@127.0.0.1:15672/api")

      r = c.overview
      expect(r.rabbitmq_version).to_not be_nil
      expect(r.erlang_version).to_not be_nil
    end
  end


  #
  # URI-only access
  #

  describe "URI-only access" do
    it "authenticates successfully" do
      c = described_class.connect("http://guest:guest@127.0.0.1:15672")

      r = c.overview
      expect(r.rabbitmq_version).to_not be_nil
      expect(r.erlang_version).to_not be_nil
    end
  end


  #
  # Overview
  #

  describe "GET /api/overview" do
    it "returns an overview" do
      r = subject.overview

      ts = r.exchange_types.map { |h| h.name }.
        sort
      ["direct", "fanout", "headers", "topic"].each do |t|
        expect(ts).to include(t)
      end

      expect(r.rabbitmq_version).to_not be_nil
      expect(r.erlang_version).to_not be_nil
    end
  end

  describe "#enabled_protocols" do
    it "returns a list of enabled protocols" do
      xs = subject.enabled_protocols

      expect(xs).to include("amqp")
    end
  end

  describe "#enabled_protocols" do
    it "returns a list of enabled protocols" do
      xs = subject.protocol_ports

      # hash of protocol => port
      expect(xs["amqp"]).to eq(5672)
    end
  end

  #
  # Nodes
  #

  describe "GET /api/nodes" do
    it "lists cluster nodes with detailed status information for each one of them" do
      nodes  = subject.list_nodes
      n      = nodes.first

      rabbit = n.applications.detect { |app| app.name == "rabbit" }
      expect(rabbit.description).to eq("RabbitMQ")

      expect(n.name).to match(/^rabbit/)
    end
  end

  describe "GET /api/node/:name" do
    it "returns status information for a single cluster node" do
      ns = subject.list_nodes
      n  = subject.node_info(ns.first.name)

      rabbit = n.applications.detect { |app| app.name == "rabbit" }
      expect(rabbit.description).to eq("RabbitMQ")

      expect(n.name).to match(/^rabbit/)
    end
  end

  #
  # Extensions
  #

  describe "GET /api/extensions" do
    it "returns a list of enabled management plugin extensions" do
      xs = subject.list_extensions

      expect(xs).to be_kind_of(Array)
    end
  end

  #
  # Definitions
  #

  describe "GET /api/definitions" do
    it "returns a list of all resources/definitions (vhosts, users, permissions, queues, exchanges, bindings, etc)" do
      await_event_propagation
      xs = subject.list_definitions

      expect(xs.bindings).to be_instance_of(Hashie::Array)
      expect(xs.queues).to be_instance_of(Hashie::Array)
      expect(xs.exchanges).to be_instance_of(Hashie::Array)
      expect(xs.users).to be_instance_of(Hashie::Array)
      expect(xs.vhosts).to be_instance_of(Hashie::Array)
    end
  end

  describe "POST /api/definitions" do
    let(:queue_name) { 'my-definition-queue' }

    let(:definition) do
      {
        :queues => [{
          :name => queue_name,
          :vhost => '/',
          durable: true,
          :auto_delete =>  false,
          :arguments => {
             "x-dead-letter-exchange" => 'dead'
          }
        }]
      }.to_json
    end

    it "returns true when successful" do
      r = subject.upload_definitions(definition)
      expect(r).to eq(true)

      subject.delete_queue("/", queue_name)
    end

{'fraction_non_alphanumeric': 0.11160660479691455, 'fraction_numerical': 0.010485717729299748, 'mean_word_length': 2.5280612244897958, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '904472', 'n_tokens_mistral': 2858, 'n_tokens_neox': 2663, 'n_words': 701}