diff --git a/lib/ssl_common.rb b/lib/ssl_common.rb index 162af874dbb..0715d119f67 100644 --- a/lib/ssl_common.rb +++ b/lib/ssl_common.rb @@ -36,6 +36,7 @@ class SSLCommon url = URI.parse(url) http = self.get_http_conn(url.host, url.port, url.scheme.downcase == 'https') req = Net::HTTP::Post.new(url.request_uri, headers) + req.basic_auth url.user, url.password if url.user req.form_data = form_data if form_data http.start {|http| http.request(req, payload) } end diff --git a/spec/lib/ssl_common_spec.rb b/spec/lib/ssl_common_spec.rb new file mode 100644 index 00000000000..a2d2def06f2 --- /dev/null +++ b/spec/lib/ssl_common_spec.rb @@ -0,0 +1,62 @@ +# +# Copyright (C) 2011 Instructure, Inc. +# +# This file is part of Canvas. +# +# Canvas is free software: you can redistribute it and/or modify it under +# the terms of the GNU Affero General Public License as published by the Free +# Software Foundation, version 3 of the License. +# +# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more +# details. +# +# You should have received a copy of the GNU Affero General Public License along +# with this program. If not, see . +# + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb') + +describe SSLCommon do + it "should work with http basic auth, username and password" do + server, server_thread, post_lines = start_test_http_server + SSLCommon.post_data("http://theusername:thepassword@localhost:#{server.addr[1]}/endpoint", + "somedata", "application/x-jt-is-so-cool") + server_thread.join + post_lines.should == [ + "POST /endpoint HTTP/1.1", + "Accept: */*", + "Content-Type: application/x-jt-is-so-cool", + "Authorization: Basic #{Base64.encode64("theusername:thepassword").strip}", + "", + "somedata"] + end + + it "should work with http basic auth, just username" do + server, server_thread, post_lines = start_test_http_server + SSLCommon.post_data("http://theusername@localhost:#{server.addr[1]}/endpoint", + "somedata", "application/x-jt-is-so-cool") + server_thread.join + post_lines.should == [ + "POST /endpoint HTTP/1.1", + "Accept: */*", + "Content-Type: application/x-jt-is-so-cool", + "Authorization: Basic #{Base64.encode64("theusername:").strip}", + "", + "somedata"] + end + + it "should work with no auth" do + server, server_thread, post_lines = start_test_http_server + SSLCommon.post_data("http://localhost:#{server.addr[1]}/endpoint", + "somedata", "application/x-jt-is-so-cool") + server_thread.join + post_lines.should == [ + "POST /endpoint HTTP/1.1", + "Accept: */*", + "Content-Type: application/x-jt-is-so-cool", + "", + "somedata"] + end +end diff --git a/spec/models/course_spec.rb b/spec/models/course_spec.rb index 3ed2180f299..9f3b82daafb 100644 --- a/spec/models/course_spec.rb +++ b/spec/models/course_spec.rb @@ -655,30 +655,6 @@ describe Course, 'grade_publishing' do PluginSetting.settings_for_plugin('grade_export')[:wait_for_success] = "no" end - def start_server - post_lines = [] - server = TCPServer.open(0) - port = server.addr[1] - post_lines = [] - server_thread = Thread.new(server, post_lines) do |server, post_lines| - client = server.accept - content_length = 0 - loop do - line = client.readline - post_lines << line.strip unless line =~ /\AHost: localhost:|\AContent-Length: / - content_length = line.split(":")[1].to_i if line.strip =~ /\AContent-Length: [0-9]+\z/ - if line.strip.blank? - post_lines << client.read(content_length) - break - end - end - client.puts("HTTP/1.1 200 OK\nContent-Length: 0\n\n") - client.close - server.close - end - return server, server_thread, post_lines - end - it 'should pass a quick sanity check' do user = User.new Course.valid_grade_export_types["test_export"] = { @@ -691,7 +667,7 @@ describe Course, 'grade_publishing' do PluginSetting.settings_for_plugin('grade_export')[:enabled] = "true" PluginSetting.settings_for_plugin('grade_export')[:format_type] = "test_export" PluginSetting.settings_for_plugin('grade_export')[:wait_for_success] = "no" - server, server_thread, post_lines = start_server + server, server_thread, post_lines = start_test_http_server PluginSetting.settings_for_plugin('grade_export')[:publish_endpoint] = "http://localhost:#{server.addr[1]}/endpoint" @course.grading_standard_id = 0 @@ -710,7 +686,7 @@ describe Course, 'grade_publishing' do PluginSetting.settings_for_plugin('grade_export')[:enabled] = "true" PluginSetting.settings_for_plugin('grade_export')[:format_type] = "instructure_csv" PluginSetting.settings_for_plugin('grade_export')[:wait_for_success] = "no" - server, server_thread, post_lines = start_server + server, server_thread, post_lines = start_test_http_server PluginSetting.settings_for_plugin('grade_export')[:publish_endpoint] = "http://localhost:#{server.addr[1]}/endpoint" @course.grading_standard_id = 0 @course.publish_final_grades(user) @@ -823,7 +799,7 @@ describe Course, 'grade_publishing' do PluginSetting.settings_for_plugin('grade_export')[:enabled] = "true" PluginSetting.settings_for_plugin('grade_export')[:format_type] = "instructure_csv" PluginSetting.settings_for_plugin('grade_export')[:wait_for_success] = "no" - server, server_thread, post_lines = start_server + server, server_thread, post_lines = start_test_http_server PluginSetting.settings_for_plugin('grade_export')[:publish_endpoint] = "http://localhost:#{server.addr[1]}/endpoint" @course.publish_final_grades(teacher.user) server_thread.join @@ -842,7 +818,7 @@ describe Course, 'grade_publishing' do "#{teacher.id},T1,#{sec4.id},,#{stud6.id},,#{Enrollment.find_by_user_id_and_course_section_id(stud6.user.id, sec4.id).id},active,\"\",90\n"] @course.grading_standard_id = 0 @course.save - server, server_thread, post_lines = start_server + server, server_thread, post_lines = start_test_http_server PluginSetting.settings_for_plugin('grade_export')[:publish_endpoint] = "http://localhost:#{server.addr[1]}/endpoint" @course.publish_final_grades(teacher.user) server_thread.join diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3757b285775..38d72b5b957 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -292,4 +292,28 @@ Spec::Runner.configure do |config| ActionController::Base.class_eval { alias_method :allow_forgery_protection, :_old_protect } end + def start_test_http_server + post_lines = [] + server = TCPServer.open(0) + port = server.addr[1] + post_lines = [] + server_thread = Thread.new(server, post_lines) do |server, post_lines| + client = server.accept + content_length = 0 + loop do + line = client.readline + post_lines << line.strip unless line =~ /\AHost: localhost:|\AContent-Length: / + content_length = line.split(":")[1].to_i if line.strip =~ /\AContent-Length: [0-9]+\z/ + if line.strip.blank? + post_lines << client.read(content_length) + break + end + end + client.puts("HTTP/1.1 200 OK\nContent-Length: 0\n\n") + client.close + server.close + end + return server, server_thread, post_lines + end + end