4

I need to receive incoming emails as multipart-formdata via a POST request from Cloudmailin. The POST resembles the following:

Parameters: {"to"=>"<email@exmaple.comt>", "from"=>"whomever@example", "subject"=>"my awesome subject line....

Actually, receiving and parsing emails is super easy because the email is just posted as params: params[:to], params[:from], etc. However, how do I simulate this POST request in rails?

I built a dummy rails app to test out Cloudmailin, so I have an actual request. However, it's a 6k character file, so I'd like to load this file as the parameters of the POST request. I've tried using the built rails post and post_via_redirect methods to load a file, but it escapes all of the parameters( \"to\"), which is no good. Any ideas?

GoodGets
  • 1,829
  • 3
  • 18
  • 21
  • http://railscasts.com/episodes/276-testing-time-web-requests and particularly https://github.com/chrisk/fakeweb – rubish Aug 22 '11 at 16:30
  • Thank you for the comment. I believe fakeweb was meant to test fetching from external API's. I need to test an incoming request. Actually, that's not true. I just need to post some params to a controller, and the rest of the test will ensure that a proper Mail was created. – GoodGets Aug 22 '11 at 16:41
  • sorry, Probably I should sleep :) – rubish Aug 22 '11 at 16:47
  • Happened to see this one as a "related question", so linking it up – it fits well here: http://stackoverflow.com/a/24353475/6962 – Henrik N Oct 19 '14 at 07:40

3 Answers3

12

So, I ended up doing:

@parameters = { "x_to_header"=>"<#{ @detail.info }>",
                "to"=>"<#{ @account.slug }@cloudmailin.net>",
                "from"=>"#{ @member.email }",
                "subject"=>"meeting on Monday",
                "plain"=>"here is my message\nand this is a new line\n\n\nand two new lines\n\n\n\nand a third new line"
              }

then just:

post "/where_ever", @parameters

seems to get the job done for now

GoodGets
  • 1,829
  • 3
  • 18
  • 21
0

I saw this answer last night when I was updating some of my own test code for Rails 3.2.8, and which uses the Mail gem, and thought I'd share what I found. The test code is for an application that needs to take a POST from Cloudmailin and then process it to create a new user with Devise, and then send a confirmation to that user, which the user can then follow to choose a password. Here is my controller spec:

require 'spec_helper'

describe ThankyouByEmailController do

  message1 = Mail.new do 

    from "Frommy McFromerton <frommy.mcfrommerton@gmail.com>"
    to "toey.receivesalot@gmail.com"
    subject "cloudmailin test"
    body 'something'

    text_part do
      body 'Here is the attachment you wanted'
    end

    html_part do
      content_type 'text/html; charset=UTF-8'
      body '<h1>Funky Title</h1><p>Here is the attachment you wanted</p>'
    end
  end

  describe "creating new users" do

    describe "unregistered FROM sender and Unregistered TO receiver" do

      it "should create 2 new users" do
        lambda do
          post :create, :message => "#{@message1}"
        end.should change(User, :count).by(2)
      end
    end
  end
end

Hope this clean up your own tests. And for anyone else interested in testing the mail gem, mikel's documentation has come a long way for same:

https://github.com/mikel/mail

Schadenfred
  • 136
  • 1
  • 9
0

A simple way would probably to execute a script in capybara. Just make sure with the @javascript tag, then load any page in your app that has jQuery installed (technically, you don't need this, but it's much easier. Then:

When /^I get a post request from Cloudmailin$/ do
  visit '/some/page/with/jquery'
  page.execute_script(%{$.post("/some/path?to=some_email&etc=etc");})
end

There's the simple post capybara method too, but I'm not too sure about how that works. Might be worth looking into.

Ramon Tayag
  • 15,224
  • 9
  • 43
  • 69