<commit_before>require 'spec_helper'

describe LXC::Server do
  it 'GET / returns some data' do
    get '/'
    last_response.status.should eq(200)
    last_response.headers['Content-Type'].should eq('application/json;encoding=utf8, charset=utf-8')
    last_response.body.should_not be_empty
  end

  it 'GET /version returns gem version' do
    get '/version'
    last_response.should be_ok
    parse_json(last_response.body)['version'].should eq(LXC::VERSION)
  end

  it 'GET /lxc_version returns installed LXC version' do
    stub_lxc('version') { fixture('lxc-version.txt') }

    get '/lxc_version'
    last_response.should be_ok
    parse_json(last_response.body)['version'].should eq('0.7.5')
  end

  it 'GET /containers returns a list of containers' do
    get '/containers'
    last_response.should be_ok
    data = parse_json(last_response.body)

    data.should be_an Array
    data.should_not be_empty
    data.first.keys.should eq(['name', 'state', 'pid'])
  end

  it 'GET /container/:name returns a single container' do
    get '/containers/app'
    last_response.should be_ok
    data = parse_json(last_response.body)

    data.should be_a Hash
    data.should_not be_empty
    data['name'].should eq('app')
    data['status'].should eq('RUNNING')
    data['pid'].should eq('2125')
  end
end<commit_msg>Handle server request errors and exceptions<commit_after>require 'spec_helper'

describe LXC::Server do
  it 'GET / returns some data' do
    get '/'
    last_response.status.should eq(200)
    last_response.headers['Content-Type'].should eq('application/json;encoding=utf8, charset=utf-8')
    last_response.body.should_not be_empty
  end

  it 'GET /version returns gem version' do
    get '/version'
    last_response.should be_ok
    parse_json(last_response.body)['version'].should eq(LXC::VERSION)
  end

  it 'GET /lxc_version returns installed LXC version' do
    stub_lxc('version') { fixture('lxc-version.txt') }

    get '/lxc_version'
    last_response.should be_ok
    parse_json(last_response.body)['version'].should eq('0.7.5')
  end

  it 'GET /containers returns a list of containers' do
    get '/containers'
    last_response.should be_ok
    data = parse_json(last_response.body)

    data.should be_an Array
    data.should_not be_empty
    data.first.keys.should eq(['name', 'state', 'pid'])
  end

  it 'GET /container/:name returns a single container' do
    get '/containers/app'
    last_response.should be_ok
    data = parse_json(last_response.body)

    data.should be_a Hash
    data.should_not be_empty
    data['name'].should eq('app')
    data['status'].should eq('RUNNING')
    data['pid'].should eq('2125')
  end

  context 'Errors' do
    class LXC::Server
      get '/exception' do
        raise RuntimeError, "Something went wrong"
      end
    end

    it 'renders error message on non-existent route' do
      get '/foo-bar'
      last_response.should_not be_ok
      last_response.status.should eq(404)
      parse_json(last_response.body)['error'].should eq('Invalid request path')
    end

    it 'renders exception message on internal server error' do
      get '/exception'
      last_response.should_not be_ok
      last_response.status.should eq(500)
      data = parse_json(last_response.body)
      data.should be_a Hash
      data.should_not be_empty
      data['error'].should_not be_nil
      data['error']['message'].should eq('Something went wrong')
      data['error']['type'].should eq('RuntimeError')
    end
  end
end
{'fraction_non_alphanumeric': 0.114097798112668, 'fraction_numerical': 0.008578781812982557, 'mean_word_length': 3.467432950191571, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '6496848', 'n_tokens_mistral': 1141, 'n_tokens_neox': 1112, 'n_words': 276}