With the impression that simply setting the request-map's :session to nil will cause a logout, my code looks like the following:
(GET "/logout" [ :as request]
(if-let [useremail (get-in request [:session :ph-auth-email])]
(-> (response {:status 200,
:body (pr-str "logged out " useremail),
:headers {"Content-Type:" "text/html"}})
(assoc request [:session nil]))))
But I get an error:
java.lang.Thread.run(Thread.java:745)
2015-02-18 09:29:05.134:WARN:oejs.AbstractHttpConnection:/logout
java.lang.Exception: Unrecognized body: {:status 200, :body "\"logged out \" \"sova\"", :headers {"Content-Type:" "text/html"}}
ring.util.response/response expects only the body as parameter since it'll build :status and :headers around it (see here). A map, however, is not a valid body - only strings, files, streams are allowed.
So, this is what causes the exception; now, on to your question: You can log out a user by setting :session to nil in your response (source) - which reduces your code to:
(GET "/logout" [:as request]
(if-let [useremail (get-in request [:session :ph-auth-email])]
{:status 200,
:body (pr-str "logged out " useremail),
:session nil, ;; !!!
:headers {"Content-Type" "text/html"}}))
Related
I have an endpoint where I can upload a text file with curl like this:
curl -X POST -H "Content-Type: multipart/form-data" -F "file=#/resources/speciesDiffusion.tree" http://localhost:4000/continuous/tree
now I need to send a similar request from a browser, but
(ajax/ajax-request
{:uri (str "http://localhost:4000" "/continuous/tree")
:method :post
:params {:treefile file}
:handler #(println %1)
:format (ajax/text-request-format)
:response-format (ajax/json-response-format {:keywords? true})})
gives me a (nicely json converted, so I got that part going, which is nice) error response:
[false {:status 500, :status-text , :failure :error, :response {:timestamp 1494279686227, :status 500, :error Internal Server Error, :exception org.springframework.web.multipart.MultipartException, :message Current request is not a multipart request, :path /continuous/tree}}]
Also, in the browser I can see that the content-type headers is not correctly set, but I couldn't get it to work with any other combination of :format and :params.
There are some examples in the README of the cljs-ajax project. For example:
(let [form-data (doto
(js/FormData.)
(.append "id" "10")
(.append "file" js-file-value "filename.txt"))]
(POST "/send-file" {:body form-data
:response-format (raw-response-format)
:timeout 100}))
https://github.com/JulianBirch/cljs-ajax
As per my comment the problem was not in the request, but rather in the f-tion that dispatched it, i.e. I was reading the file content instead of sending the raw object like here:
(defn handle-file-upload [evt]
(let [target (.-currentTarget evt)
js-file (-> target .-files (aget 0))]
(do
(re-frame/dispatch [:post-file {:file js-file
:name (.-name js-file)}])
(set! (.-value target) ""))))
(defn upload-button []
(fn []
[:input {:class "none"
:id "file-upload"
:type "file"
:on-change #(handle-file-upload %)}]))
where
:post-file
is an event which invokes the handler which does the POST request.
Consider the following compojure routing:
(defroutes main-routes
(POST "/something" r {:body (prn-str (:params r))}))
(def handler
(-> main-routes
(wrap-params)))
When testing this with curl I'm getting the desired result:
curl -d "a=b" localhost:3000/something
{"a" "b"}
The Post parameters are read by compojure and wrapped to the params-map.
However this does not work with an ajax request initiated by the cljs-ajax library:
(POST "/something" {:handler #(js/alert %)
:params {"a" "b"}})
It alerts "{}". When changing the code to use GET, it works however. I guess this is due to the fact, that the browser sends the body as an input stream and not as plain text. But I'm not sure and I don't know how to fix this.
It looks like cljs-ajax is sending a transit-formatted request and response by default. (See :format and :response-format defaults here). You might try specifying an explicit json response in the request map -
{:handler #(js/alert %)
:params {"a" "b"}
:response-format :json}
I'd like to see the backtrace of error when I run this code.
(make-network-process :name (dbgp-make-listner-name 10004)
:server 1
:service 10004
:family 'ipv4
:nowait t
:noquery t
:filter 'dbgp-comint-setup
:sentinel 'dbgp-listener-sentinel
:log 'dbgp-listener-log)
https://github.com/gregsexton/ob-ipython/issues/4
This shows that I can see what's going on under make-network-process.
Debugger entered--Lisp error: (file-error "make client process failed" "connection refused" :name "localhost" :buffer # :host "localhost" :service 9988 :nowait nil)
make-network-process(:name "localhost" :buffer # :host "localhost" :service 9988 :nowait nil)
open-network-stream("localhost" # "localhost" 9988 :type plain :nowait nil)
byte-code . . .
url-open-stream("localhost" # "localhost" 9988)
url-http-find-free-connection("localhost" 9988)
url-http([cl-struct-url "http" nil nil "localhost" 9988 "/execute/default" nil nil t nil t] #128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)")
url-retrieve-internal("http://localhost:9988/execute/default" #128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)" nil nil)
url-retrieve("http://localhost:9988/execute/default" #[128
I tried
toggle-debug-on-error
edebug-eval-defun
But I can't see the backtrace..
on *Backtrace* buffer, I can see
Debugger entered: nil
(progn (debug) (make-network-process :name (dbgp-make-listner-name 10004) :server 1 :service 10004 :family (quote ipv4) :nowait t :noquery t :filter (quote dbgp-comint-setup) :sentinel (quote dbgp-listener-sentinel) :log (quote dbgp-listener-log)))
eval((progn (debug) (make-network-process :name (dbgp-make-listner-name 10004) :server 1 :service 10004 :family (quote ipv4) :nowait t :noquery t :filter (quote dbgp-comint-setup) :sentinel (quote dbgp-listener-sentinel) :log (quote dbgp-listener-log))) nil)
edebug-eval-defun(nil)
apply(edebug-eval-defun nil)
eval-defun(nil)
funcall-interactively(eval-defun nil)
call-interactively(eval-defun record nil)
command-execute(eval-defun record)
helm-M-x(nil "eval-defun")
funcall-interactively(helm-M-x nil "eval-defun")
call-interactively(helm-M-x nil nil)
command-execute(helm-M-x)
While the first trace shows what's going on behind make-network-process, my trace doesn't go deeper..
So the question is how to get more detail in the backtrace.
For byte-compiled elisp functions, loading the uncompiled version of the library in question will generally give you more detail, as the debugger can then show you exactly where in the function things happened.
However in this instance, make-network-process is a C function, so what you see now is all you're going to get out of the elisp debugger.
You'll have to examine/debug the source code in process.c if you want to know more.
I have some code that looks like this:
while response.droplet.status != env["user_droplet_desired_state"] do
sleep 2
response = ocean.droplet.show env["droplet_id"]
say ".", nil, false
end
The idea being you can set the app to wait until the server is in a certain state (eg. restart it, then watch it until it's active again)
However, I'm using webmock in the tests, and I can't figure out a way to give a different response the second time.
For example, code like this:
stub_request(:get, "https://api.digitalocean.com/v2/droplets/6918990?per_page=200").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer foo', 'Content-Type'=>'application/json', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => fixture('show_droplet_inactive'), :headers => {})
stub_request(:get, "https://api.digitalocean.com/v2/droplets/6918990?per_page=200").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer foo', 'Content-Type'=>'application/json', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => fixture('show_droplet'), :headers => {})
With the idea being "First time mark as in-active so the loop goes through one-time, then mark as active afterwards"
The documentation says that stubs are just done as "Last one found will work":
Always the last declared stub matching the request will be applied
i.e:
stub_request(:get, "www.example.com").to_return(:body => "abc")
stub_request(:get, "www.example.com").to_return(:body => "def")
Net::HTTP.get('www.example.com', '/') # ====> "def"
Is it possible to model multiple calls to the same endpoint with different results in webmock?
If you pass multiple arguments to #to_return, it will respond each time with the next response, and then just keep returning the last one over and over. For example:
require 'webmock/rspec'
require 'uri'
describe "something" do
it "happens" do
stub_request(:get, 'example.com/blah').
to_return({status: 200, body: 'ohai'}, {status: 200, body: 'there'})
puts Net::HTTP.get(URI('http://example.com/blah'))
puts Net::HTTP.get(URI('http://example.com/blah'))
puts Net::HTTP.get(URI('http://example.com/blah'))
puts Net::HTTP.get(URI('http://example.com/blah'))
end
end
When run as rspec <file>, this will print:
ohai
there
there
there
I'm trying to use webmock with rspec to stub out requests to Aws but I can't seem to get the regex to work for SQS polling. If I run rspec, webmock generates a 'correct' stub for me to use in a before(:each) block, in my spec_helper.rb like this:
You can stub this request with the following snippet:
stub_request(:post, "https://sqs.us-west-2.amazonaws.com/123456789012/backlog").
with(:body => "Action=ReceiveMessage&AttributeName.1=All&MaxNumberOfMessages=1&MessageAttributeName.1=All&QueueUrl=https%3A%2F%2Fsqs.us-west-2.amazonaws.com%2F123456789012%2Fbacklog&Version=2012-11-05&VisibilityTimeout=0&WaitTimeSeconds=20",
:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'', 'Authorization'=>'AWS4-HMAC-SHA256 Credential=MY_ACCESS_KEY/20150726/us-west-2/sqs/aws4_request, SignedHeaders=content-type;host;user-agent;x-amz-content-sha256;x-amz-date, Signature=large_alpha-numeric-signature', 'Content-Length'=>'224', 'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8', 'Host'=>'sqs.us-west-2.amazonaws.com', 'User-Agent'=>'aws-sdk-ruby2/2.1.7 ruby/2.2.2 x86_64-darwin14', 'X-Amz-Content-Sha256'=>'69336339ae76cf370477d4dsaf667as0b5dd8d25762c7c78sad8a', 'X-Amz-Date'=>'20150726T143009Z'}).
to_return(:status => 200, :body => "", :headers => {})
So in my spec_helper.rb I have
RSpec.configure do |config|
config.before(:each) do
stub_request(:post, "https://sqs.us-west-2.amazonaws.com/123456789012/backlog").
with(:body => "Action=ReceiveMessage&AttributeName.1=All&MaxNumberOfMessages=1&MessageAttributeName.1=All&QueueUrl=https%3A%2F%2Fsqs.us-west-2.amazonaws.com%2F123456789012%2Fbacklog&Version=2012-11-05&VisibilityTimeout=0&WaitTimeSeconds=20",
:headers => {'Accept'=>'*/*',
'Accept-Encoding'=>'',
'Authorization'=>"AWS4-HMAC-SHA256 Credential=MY_ACCESS_KEY/20150726/us-west-2/sqs/aws4_request, SignedHeaders=content-type;host;user-agent;x-amz-content-sha256;x-amz-date, Signature=" + /"^[a-zA-Z0-9]*$"/,
'Content-Length'=>'224',
'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8',
'Host'=>'sqs.us-west-2.amazonaws.com',
'User-Agent'=>'aws-sdk-ruby2/2.1.7 ruby/2.2.2 x86_64-darwin14',
'X-Amz-Content-Sha256'=>'694236339ae76cf370477d4dsaf667as0b5dd8d25762c7c78sad8a',
'X-Amz-Date'=>""+ /"^[a-zA-Z0-9]*$"/}).
to_return(:status => 200, :body => "", :headers => {})
end
The areas I'm trying to use regex against are the Signature and the X-Amz-Date because they're the only two that seem to change between different attempts to run the rspec.
The problem is the regex seems to not be working because even though I've added it into the spec_helper.rb, every time I run the suite, I get back the recommended stub from webmock instead of a passing or failing test. It should be passing at this point, from what I understand from the webmock docs and several tutorials.
How should I change this to get webmock to work for my test suite against Aws SQS polling?
I've been bashing my head against my desk for a few days now so any help is much appreciated.
Signature is likely generated using a byproduct of Time.now and I'm guessing you don't actually want to test for that. Instead simply do:
stub_request(:post, "https://sqs.us-west-2.amazonaws.com/123456789012/backlog").and_return(:status => 200, :body => "", :headers => {})
If you want to be even less specific on the URL (like ommiting that ID) you can even use a regex match:
stub_request(:post, /amazonaws.com/).and_return(:status => 200, :body => "", :headers => {})
I have the same problem and have the solution only for 'X-Amz-Date'.
Since it is date in special format, use Timecop.freeze block around your mock and method call.
Timecop.freeze do
stub_request(:get, "https://s3.eu-central-1.amazonaws.com/test_bucket/test").
with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'', 'Authorization'=>'AWS4-HMAC-SHA256 Credential=access_key/20190814/eu-central-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=9e6a6a209a0cb05346a058c12ef706ebff185ae3b72f3e542e1becbc97e8ea7a', 'Content-Length'=>'0', 'Content-Type'=>'', 'Host'=>'s3.eu-central-1.amazonaws.com', 'User-Agent'=>'aws-sdk-ruby2/2.9.44 ruby/2.6.3 x86_64-darwin18 resources', 'X-Amz-Content-Sha256'=>'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'X-Amz-Date'=>"#{time.utc.iso8601(0).gsub(/[^\p{Alnum}]/, '')}"}).
to_return(status: 200, body: "", headers: {})
AwsService.bucket.object("test").get
end
The regexp leaves only alphanumerical characters and Timecop takes care of the time.