<commit_before>require 'minitest'
require 'minitest/hooks/default'
require 'httpclient'
require 'sqrl/key/identity_unlock'
require 'sqrl/key/unlock_request_signing'
require 'sqrl/client_session'
require 'sqrl/query_generator'
require 'sqrl/response_parser'
require 'sqrl/base64'
require 'sqrl/check/version'

module SQRL::Check::Server; end

class SQRL::Check::Server::Test < Minitest::Test
  include Minitest::Hooks

  URL = ENV['SQRL_CHECK_URL'] || 'http://localhost:3000'
  SignedCert = !!ENV['SQRL_CHECK_SIGNED_CERT']

  SqrlHeaders = {'Content-Type' => 'application/x-www-form-urlencoded'}
  SqrlRequest = {
    :agent_name => "SQRL/1 SQRL::Check/#{SQRL::Check::VERSION}",
    :default_header => SqrlHeaders,
  }
  def post(session)
    req = SQRL::QueryGenerator.new(session)
    req = yield req if block_given?
    h = HTTPClient.new(SqrlRequest)
    h.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE unless SignedCert
    res = h.post(req.post_path, req.post_body)

    SQRL::ResponseParser.new(session, res.body)
  end

  def create_session(url, imks)
    url = upgrade_url(url)
    SQRL::ClientSession.new(url, imks)
  end

  ScanRequest = {
    :agent_name => "SQRL::Check/#{SQRL::Check::VERSION}",
  }
  def upgrade_url(url)
    return url unless url.start_with?('http')
    h = HTTPClient.new(ScanRequest)
    h.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE unless SignedCert
    res = h.get(url)
    matches = res.body.match(/"(s?qrl:\/\/[^"]+)"/m)
    if matches
      if matches.length > 2
        puts "multiple matches"
        (1...matches.length).to_a.each do |i|
          puts matches[i]
        end
      end
      return matches[1].gsub('&amp;', '&')
    else
      return url
    end
  end

  def self.assert_flags(response_var, flags)
    flags.each_pair do |name, value|
      define_method "test_#{response_var}_#{name}_is_#{value}" do
        assert_equal(value, send(response_var).send("#{name}?"))
      end
    end
  end

  def self.assert_known_version(response_var)
    define_method :test_known_version do
      assert_equal('1', send(response_var).params['ver'], 'check is only defined for version 1')
    end
  end
end

Minitest.autorun
<commit_msg>only autorun if run as a test<commit_after>require 'minitest'
require 'minitest/hooks/default'
require 'httpclient'
require 'sqrl/key/identity_unlock'
require 'sqrl/key/unlock_request_signing'
require 'sqrl/client_session'
require 'sqrl/query_generator'
require 'sqrl/response_parser'
require 'sqrl/base64'
require 'sqrl/check/version'

module SQRL::Check::Server; end

class SQRL::Check::Server::Test < Minitest::Test
  include Minitest::Hooks

  URL = ENV['SQRL_CHECK_URL'] || 'http://localhost:3000'
  SignedCert = !!ENV['SQRL_CHECK_SIGNED_CERT']

  SqrlHeaders = {'Content-Type' => 'application/x-www-form-urlencoded'}
  SqrlRequest = {
    :agent_name => "SQRL/1 SQRL::Check/#{SQRL::Check::VERSION}",
    :default_header => SqrlHeaders,
  }
  def post(session)
    req = SQRL::QueryGenerator.new(session)
    req = yield req if block_given?
    h = HTTPClient.new(SqrlRequest)
    h.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE unless SignedCert
    res = h.post(req.post_path, req.post_body)

    SQRL::ResponseParser.new(session, res.body)
  end

  def create_session(url, imks)
    url = upgrade_url(url)
    SQRL::ClientSession.new(url, imks)
  end

  ScanRequest = {
    :agent_name => "SQRL::Check/#{SQRL::Check::VERSION}",
  }
  def upgrade_url(url)
    return url unless url.start_with?('http')
    h = HTTPClient.new(ScanRequest)
    h.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE unless SignedCert
    res = h.get(url)
    matches = res.body.match(/"(s?qrl:\/\/[^"]+)"/m)
    if matches
      if matches.length > 2
        puts "multiple matches"
        (1...matches.length).to_a.each do |i|
          puts matches[i]
        end
      end
      return matches[1].gsub('&amp;', '&')
    else
      return url
    end
  end

  def self.assert_flags(response_var, flags)
    flags.each_pair do |name, value|
      define_method "test_#{response_var}_#{name}_is_#{value}" do
        assert_equal(value, send(response_var).send("#{name}?"))
      end
    end
  end

  def self.assert_known_version(response_var)
    define_method :test_known_version do
      assert_equal('1', send(response_var).params['ver'], 'check is only defined for version 1')
    end
  end
end

if $PROGRAM_NAME.match(/_test\.rb|rake_test_loader\.rb/)
  Minitest.autorun
end

{'fraction_non_alphanumeric': 0.13767629281396912, 'fraction_numerical': 0.005372733378106112, 'mean_word_length': 3.8302702702702702, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5307860', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1587, 'n_words': 299}