luckyframework / lucky

A full-featured Crystal web framework that catches bugs for you, runs incredibly fast, and helps you write code that lasts.
https://luckyframework.org
MIT License
2.57k stars 156 forks source link

Not possible to send raw body in the ApiClient in tests #1679

Closed confact closed 1 year ago

confact commented 2 years ago

Describe the bug I am doing a app with json streaming (multiple jsons separated with lines (\n)) and had no way to send it in as params got parsed to json and would not work.

To Reproduce Steps to reproduce the behavior:

    test_data = <<-JSON
      { "event_id": "#{event_id}"}
      { "type": "event"}
      { "event_id": "#{event_id}", "type": "event", "platform": "", "timestamp": #{Time.utc.to_unix}, "message": "message"}

    JSON

    client = ApiClient.new
    response = client.exec(Api::Events, test_data)

Will give error on method with those params doesn't exist.

By adding these methods in ApiClient, it works:

  def exec(action : Lucky::Action.class, body : String) : HTTP::Client::Response
    exec(action.route, body)
  end

  def exec(route_helper : Lucky::RouteHelper, body : String) : HTTP::Client::Response
    @client.exec(method: route_helper.method.to_s.upcase, path: route_helper.path, body: body)
  end

Expected behavior Accept sending in a string in the body for testing. My suggestion is to add those methods to be default in the builtin BaseHTTPClient

Versions (please complete the following information):