Giter VIP home page Giter VIP logo

behapi's Introduction

Behapi

Behat extension to help write describe features related to HTTP APIs.

PHP 7.3, Behat 3.7 and a discoverable php-http client are required to make this extension work.

Installing this extension requires you to require taluu/behapi and an implementation of a http client (providing psr/http-client-implementation ^1.0, psr/http-factory-implementation ^1.0 and psr/http-message-implementation ^1.0).

You can find some examples on the examples/ directory.

Howto

Add this in your behat.yml (it's for the default configuration but you can use it for any configurations actually) :

default:
  suites:
    main:
      paths: ['%paths.base%/features']
      services: '@Behapi\Container'
      autowire: true

      contexts:
        #- List
        #- your
        #- contexts
        #- here

        # examples :
        - Behapi\Http\RequestContext: ~
        - Behapi\Http\ResponseContext: ~

  extensions:
    Behapi\Behapi:
      base_url: 'http://localhost'

The base_url is the only requirement in the config for this extension to work.

There are other configurations keys, such as which formatter to use in a debug environment, which headers you want to output in request or response while debugging ; Use the --config-reference flag when invoking behat to have more information on the available configuration.

After having installed the extension, you can then use the provided contexts such as the Behapi\Http\RequestContext for the http api operations. In order to use them, you need to use behapi's container (@Behapi\Container), or a container capable of using behapi's container.

Some services are provided to be injected in contexts, which are the following:

  • @Http\Client\Common\PluginClientBuilder, which will build a Http\Client\Common\PluginClient when needed
  • @Behapi\HttpHistory\History, which is a sort of a container with the last requests done and last responses received
  • @Http\Message\MessageFactory
  • @Http\Message\StreamFactory

Note: You don't really need to bother with the services names, as they are compatible with behat's auto-wiring feature.

In order to enable the Json assertions, you need to use the Behapi\Context\Json context. Note that if you use the json context, you should have used the client provided by the client builder used in the Behapi\Http\RequestContext context.

If you need to play with the request being built, or the response created when the request is sent, you need to inject the @Behapi\HttpHistory\History. It is automatically reseted between scenarios (and scenarios outlines)

A documentation will be made (soon hopefully) with more details.

Contributing

Contributing (issues, pull-requests) are of course always welcome ! Be sure to respect the standards (such as psr-2, ... etc), follow proper git etiquette (atomic commits, ...), proper conduct too and it should be fine !

Thanks

This extension was made while I was working at @Wisembly, and heavily used for writing our features and integration tests. Special thanks goes to @lunika, @rgazelot and @krichprollsch, who helped conceived this extension, and also pushed me to open-source it.

Badges

Type Coverage

behapi's People

Contributors

guillaumepotier avatar krichprollsch avatar lunika avatar rgazelot avatar taluu avatar titoine avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

behapi's Issues

Tests

Add some tests. :}

Use PSR-18 / PSR-17 instead of Http-Plug

The psrs were released in last july, so maybe it'd be time to use this in Behapi.

But that maybe would introduce bc breaks, so it will be for 0.13 (or 1.0 ?)

Debug mode do not works as expected

Currently using the debug mode with --api-debug nor @debug does not works as expected.
The debug context is loaded via behat.yml

default:                                                                         
  formatters:                                                                    
    progress: true                                                               
  suites:                                                                        
    main:                                                                        
      paths: [%paths.base%/features/specs]                                       
      services: "@behapi.container"                                              
                                                                                 
      contexts:                                                                  
        - Behapi\Context\Rest:                                                   
          - "@http.client"                                                       
          - "@http.stream_factory"                                               
          - "@http.message_factory"                                              
          - "@http.history"                                                      
                                                                                 
        - Behapi\Context\Json:                                                   
          - "@http.history"                                                      
                                                                                 
        - Behapi\Context\DebugRest:                                              
          - "@debug"                                                             
          - "@http.history"                                                      
                                                                                 
  extensions:                                                                    
    Behapi\Extension\Behapi:                                                     
      base_url: 'http://nginx/app_dev.php'  

But the output seems not good:

/srv $  ./vendor/bin/behat features/specs/api/decoder.feature:18 --api-debug 
@rest
Feature: Test data request decoder using a debug endoint
  In order to test the data decoder
  As a api user
  I need to be able to send data

  Scenario: send json data via GET should be ignored        # features/specs/api/decoder.feature:18
    When I create a "GET" request to "/debug/api/echo-data" # Behapi\Context\Rest::createARequest()
    And I set the following body:                           # Behapi\Context\Rest::setTheBody()
      """
       {"foo": "bar"}
      """
    And I send the request                                  # Behapi\Context\Rest::sendRequest()
    Then the status code should be 200                      # Behapi\Context\Rest::statusCodeShouldBe()
    And the response should be a valid json response        # Behapi\Context\Json::responseIsValidjson()
    And in the json, "data.foo" should be equal to "bar"    # Behapi\Context\Json::theJsonPathShouldBeEqualTo()
      PropertyAccessor requires a graph of objects or arrays to operate on, but it found type "NULL" while trying to traverse path "data.foo" at property "foo". (Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException)

--- Failed scenarios:

    features/specs/api/decoder.feature:18

1 scenario (1 failed)
6 steps (5 passed, 1 failed)
0m0.14s (9.17Mb)

Migrate to Behat 3.3

So many sexy things in it (helper container <3), and also a bunch of deprecated stuff to remove...

Use a real printer for debug

Instead of forcing a format (eg through behapi: { debug: { formatter: 'pretty' } } ), we should use the current printer to print the data correctly, so that it works for all outputs.

Use Symfony HttpClient instead of HttpPlug ?

This would mean a refactoring of things and how the request is handled though.

Or I could keep using http-plug / psr-17/ psr-18 things, and see on the discoverable clients how to integrate symfony client (I'd rather do that actually).

Use EventDispatcher to add information to requests

Adding an event dispatcher could be useful, if we need to (e.g) add some headers, ... and so on, rather than having to listen to a @BeforeStep which may not be quite efficient.

Firing some events here and there could be useful.

README

Add a README (How tos, where does this project comes from, .... etc)

Discuss about @api tag

As discussed, we have to use the tag @api to use --api-debug and the @debug tag.

Baptiste Clavié [6:18 PM] 
je voulais avoir genre soit `@rest` soit `@api`

[6:18]  
mais je pense que je vais opter pour `@rest`

[6:18]  
(parce que `DebugRest`)

[6:18]  
ou alors `DebugApi`

[6:19]  
voir `DebugHttp` en fait, ca devrait avoir plus de sens

Remy Gazelot [6:19 PM] 
+1

Baptiste Clavié [6:20 PM] 
du coup faudrait que j'introduise un tag `@http` (edited)

Remove Twig support

It is a source of bug with the autowiring (cf Behat/Behat#1080), and also I'm not quite sure it is necessary to support it out of the box. It would also simplify all the stuff like a lot.

Using instead an extension or something along those lines would be better IMO.

Support namespaces Twig services

Currently, only not-namespaced twig classes are supported (Twig_Environement and not Twig\Environment). Need to fix that... maybe.

Maybe, because I doubt that namespaced twig classes are used, especially as they are only aliases extending the original twig classes.

form data support

Support form data support for the "parameters" (will probably though another step than the current "parameter" query).

PHP 8.0 Support ?

Currently migrating our project on PHP 8.0, will behapi natively support it soon ?

Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.