How can I test WebSockets (using Pusher) with RSpec? - ruby-on-rails

I am looking for successful methods for testing WebSocket push events using RSpec. My application currently uses Pusher App, but more broad information relating to WebSockets is most welcome.
Ideally, I'd like something as simple as:
parsed_body = JSON.parse(response.body)
parsed_body["error"].should == "xyzError"
...which I have found to be an awesomely convenient way to test for JSON responses.
Sincere thanks in advance.

Tristan Dunn came out with this awesome gem: https://github.com/tristandunn/pusher-fake

WebSocket server is remote service. It's good way to stub any requests to remote services and mock responses from these ones.

Related

What is the best way to send a SOAP request in Ruby on Rails?

I want to send a SOAP request to a server that requires authentication. I've tried doing some of the methods in the HTTP:NET documentation but basic auth won't work. What would be the best way to contact this server with a user agent? I usually always get a connection time out error (500).
Savonrb gem will be helpful for you in this case.
It is a ruby SOAP client to make SOAP communications easier. Also, check out this Railscasts episode to get started.

How to handle INCOMING HTTP/S GET requests in my Rails 4 app

I am implementing SMS paying sistem. User sends message with some code and buys some features on my site.
But to do that I need to handle 2 HTTP/S GET requests and get information from them.
First request is like this :
http://www.yourpage.com/sms/receive.php?sms-id=%sms-id%&operator-id=%operator-id%&from=%from%&to=%to%&text=%text%
How would be easiest way to handle theese requests and based on them make changes in my database ?
SO far I googled "HOW to handle HTTP/S requests in my Rails app" I got zero usefull sources. I got only sources that explains how to make HTTP/S requests but not how to handle them and use.
Is there any suggestion to this problem ?
Thanks in advance!
A Rails controller handles incoming HTTP requests. To generate one, run the following from the command line:
rails generate controller sms receive
This will create an SmsController.rb file with a function named receive.
In that function, do something like this:
def receive
sms_id=params[:sms_id]
... do something ...
end
and I'd recommend learning Rails basics :)

Grails testing Curl like post

I tend to test my API using Curl.
Is there a way in Grails that I can make a test just posts to my API and then I can evaluate the results.
I have tried Spock testing and this didn't work.
I cannot seem to find a simple example on the internet.
Has anyone done similar?
It really depends on what kind of testing you want to do. If you want to send requests to your app using curl you can. I do that for a lot of things. If you want automated tests, you can do that too. Spock is the default unit testing framework but you could use others.
See the controller unit testing docs at http://grails.org/doc/latest/guide/testing.html#unitTestingControllers. There are examples there which show how to test different HTTP request methods, JSON and XML request bodies, JSON and XML responses, AJAX, mime types, etc.
I hope that helps.

Rails Notification Without Page Refresh

I created a simple private messaging app through rails, but I am trying to get it to simply show that new message has arrived without the person having to refresh the page.
I am looking at State Machine because I am thinking that it might be a part of building something like this. Can anyone push me in the right direction?
Thanks.
The simplest way to do that using javascript is to periodically send requests to the server using setTimeout + $.get().
Another a little bit more complicated way is to use gems like faye . There is nice railscast on this theme.
Take a look at any of the messaging protocols like:
Faye
Socket.IO
or hosted sollutions like
PubNub or Pusher

Mock out Curl::Easy.perform? (in Curb)

Is there a way to mock out Curb's Easy.perform method for unit testing? I use this to hit Facebook's graph API, and none of the http mock libs seem to support Curb.
What's the best approach here?
WebMock has recently added Curb support. :)
http://github.com/bblimke/webmock
I really think fakeWeb is a great way to fake out network calls; Whatever HTTP library you use, you'll just specify what response text and code you want to receive.
Described as:
FakeWeb is a helper for faking web
requests in Ruby. It works at a global
level, without modifying code or
writing extensive stubs
Example from the github repository:
FakeWeb.register_uri(:get, "http://example.com/test1", :string => "Hello World!")
Net::HTTP.get(URI.parse("http://example.com/test1"))
=> "Hello World!"
Net::HTTP.get(URI.parse("http://example.com/test2"))
=> FakeWeb is bypassed and the response from a real request is returned

Resources